I\'m trying to make a shared service for my app.
import { Injectable } from \'@angular/core\';
@Injectable()
export class SharedService {
testService() {
I believe you use latest version and want to use singleton service.
For that you have to register your service in @NgModule({})
as shown here,
import {Sharedservice} from 'valid path';
@NgModule({
imports:[BroswerModule],
...
providers:[SharedService]
})
Now, In child and parent component just import Sharedservice at the top of the file.
NOTE : Remove providers:[SharedService]
from each component.
You need to set who's the provider for that service with providers
keyword in @Component
or @NgModule
annotation. This is well documented already zilion times, see:
https://angular.io/docs/ts/latest/guide/dependency-injection.html#!#injector-providers
https://angular.io/docs/ts/latest/guide/ngmodule.html#!#providers
https://angular.io/docs/ts/latest/cookbook/component-communication.html
https://angular.io/docs/ts/latest/cookbook/dependency-injection.html