Shared service in Angular2

后端 未结 2 1210
感情败类
感情败类 2021-01-27 18:32

I\'m trying to make a shared service for my app.

import { Injectable } from \'@angular/core\';

@Injectable()
export class SharedService {
  testService() {
            


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-27 19:12

    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

提交回复
热议问题