DevError404
DevError404

Reputation: 147

Angular : initialisation issue of Property

I am getting error of initialisation in my component.ts file

My model is

import { Account } from './account.model';

export interface Owner{
    id: string;
    name: string;
    dateOfBirth: string;
    address: string;

    accounts?: Account[];
}

And I am using it in component.ts file as :

export class OwnerDetailsComponent implements OnInit {
  public owner: Owner;
  public errorMessage: string = '';

  constructor(private repository: RepositoryService, private router: Router, 
              private activeRoute: ActivatedRoute) { }

  ngOnInit() {
    this.getOwnerDetails()
  }

  getOwnerDetails = () => {
    let id: string = this.activeRoute.snapshot.params['id'];
    let apiUrl: string = `my API url`;

    this.repository.getData(apiUrl)
    .subscribe(res => {
      this.owner = res as Owner;
    })
  }

}

I am getting error as : Property 'owner' has no initializer and is not definitely assigned in the constructor.

Please note: I don't want to initialise it with undefined

Please suggest how to remove this error.

Upvotes: 0

Views: 672

Answers (1)

ammadkh
ammadkh

Reputation: 597

Add (!) sign after property:

owner!: Owner;

or you can disable strict typing in tsconfig.json file;

"angularCompilerOptions": {
    //   ...
    "strictPropertyInitialization": false
    //   ...   }

Upvotes: 1

Related Questions