2

How can inject multiple values to my service if I have constructor like this

@Injectable()
export class MyService implements OnInit {
    constructor(private http: Http, @Inject('name') @Optional() public name?: string) {            
}

and inside AppModule I tried following

...
providers: [
    SecurityService, providers:[
      {  provide: 'username', useValue: ''  },
      {  provide: 'http', useValue: Http  }
    ],
..

but I'm getting syntax error in line SecurityService, providers:[

Cannot find name providers

1 Answer 1

4

Just pass in the dependencies as an array (you don't need to destructure it):

...
providers: [
    SecurityService, 
    [
      {  provide: 'username', useValue: ''  },
      {  provide: 'http', useValue: Http  }
    ],
...

Or go ahead and destructure it for the sake of cleaner code:

...
providers: [
    SecurityService, 
    {  provide: 'username', useValue: ''  },
    {  provide: 'http', useValue: Http  }
...
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, now I realize that I cannot use this simple http to inject Http. Any ideas on that?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.