Shared service in Angular2

后端 未结 2 1215
感情败类
感情败类 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:05

    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.

提交回复
热议问题