Issue
constructor (service: MyService) {}
ngOnInit() {
this.service.init();
}
In the above code, I'm getting:-
Property `service` does not exist on type 'MyComponment'
Yet, if I declare service as private it works. What is going on here?
--
[Angular 8.2.12, TypeScript 3.5.3]
Solution
From the docs:
Parameter properties are declared by prefixing a constructor parameter with an accessibility modifier or
readonly, or both. Usingprivatefor a parameter property declares and initializes a private member; likewise, the same is done forpublic,protected, andreadonly.
Without the prefix, the constructor parameter is nothing more than a method parameter, and you would have to manually assign it to a declared class property from the constructor itself.
Answered By - jitender
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.