I have a service defined in my Angular2 application (2.0.0-beta.0). This is something like this:
import {Injectable} from "angular2/core";
@Injectable()
export class MyService {
constructor() {
}
getSomething() {
return 'something';
}
}
I listed it in my bootstrap () function in my main application file so that it is available to my code as a whole:
bootstrap(App, [MyService, SomeOtherService, ROUTER_DIRECTIVES[);
Sometimes I cannot use the service in the component, although I have something like myService:MyServicein a function constructor(), for example:
import {MyService} from '../services/my.service';
@Component({
selector: 'my-component',
directives: [],
providers: [],
pipes: [],
template: `
<div><button (click)="doStuff()">Click Me!</button></div>
`
})
export MyComponent {
constructor(myService:MyService) {} // note the private keyword
doStuff() {
return this.myService.getSomething();
}
}
In other places, it works great. In places where this does not work, I get a message, for example, if I try to access it:
EXCEPTION: TypeError: Cannot read property 'getSomething' of undefined
This basically means that the service has not been introduced.
What makes it not to enter?
source
share