Devstar34
Devstar34

Reputation: 1097

Angular 5- How to add multiple parameters to constructor?

I need to add both my service as well as an authentication service to my app.component.ts, but I can't seem to add both in my constructor parameters. How can I do that and still have both available to the whole application? Here's what I have:

constructor(private _dataService: DataService, authToken: Angular2TokenService) {
     this.authToken.init(environment.token_auth_config)
 }

The error is: Property 'authToken' does not exist on type 'AppComponent'.

All necessary items have already been imported.

How can I write both in? Most answers I'm finding are for older versions of Angular. Thanks.

Upvotes: 1

Views: 4969

Answers (2)

Suren Srapyan
Suren Srapyan

Reputation: 68685

You have used this with authToken - this means that your authToken is declared as property of your component but currently it is not like that so why you get the error.

You need to add private or another access modifier before the authToken. This tells that the parameter in the constructor is declared as a property of the component like _dataService

constructor(private _dataService: DataService, private authToken: Angular2TokenService) {
     this.authToken.init(environment.token_auth_config)
}

Upvotes: 2

Amardeep Bhowmick
Amardeep Bhowmick

Reputation: 16908

Add private before authToken. Adding private makes it a property of the Component class. Then you can refer to the authToken with the this keyword.

constructor(private _dataService: DataService, private authToken: Angular2TokenService) {
     this.authToken.init(environment.token_auth_config)
 }

Upvotes: 7

Related Questions