【问题标题】:Angular 2 - Using Shared ServiceAngular 2 - 使用共享服务
【发布时间】:2016-03-27 19:03:18
【问题描述】:

看起来共享服务是解决许多情况的最佳实践,例如组件之间的通信或替换角度 1 的旧 $rootscope 概念。我正在尝试创建我的,但它不起作用。有什么帮助吗?太棒了!!!

app.component.ts

import {Component} from 'angular2/core';
import {OtherComponent} from './other.component';
import {SharedService} from './services/shared.service';
@Component({
selector: 'my-app',
providers: [SharedService],
directives: [OtherComponent],
template: `
    <button (click)="setSharedValue()">Add value to Shared Service</button>
    <br><br>
    <other></other>
`
})
export class AppComponent { 
data: string = 'Testing data';
setSharedValue(){
    this._sharedService.insertData(this.data);
    this.data = '';
    console.log('Data sent');
}
constructor(private _sharedService: SharedService){}
}

other.component.ts

import {Component, OnInit} from "angular2/core";
import {SharedService} from './services/shared.service';
@Component({
selector : "other",
providers : [SharedService],
template : `
I'm the other component. The shared data is: {{data}}
`,
})
export class OtherComponent implements OnInit{
data: string[] = [];
constructor(private _sharedService: SharedService){}
ngOnInit():any {
    this.data = this._sharedService.dataArray;
}
}

【问题讨论】:

    标签: angular angular2-services


    【解决方案1】:

    大多数时候,您需要在引导应用程序时定义共享服务:

    bootstrap(AppComponent, [ SharedService ]);
    

    不要在组件的providers 属性中再次定义它。这样,您将拥有整个应用程序的单个服务实例。


    在您的情况下,由于 OtherComponent 是您的 AppComponent 的子组件,因此只需像这样删除 providers 属性:

    @Component({
      selector : "other",
      // providers : [SharedService], <----
      template : `
        I'm the other component. The shared data is: {{data}}
      `,
    })
    export class OtherComponent implements OnInit{
      (...)
    }
    

    这样,它们将为两个组件共享相同的服务实例。 OtherComponent 将使用父组件中的一个 (AppComponent)。

    这是因为 Angular2 的“分层注入器”特性。有关更多详细信息,请参阅此问题:

    【讨论】:

    • 你的速度太快了 :P 这是一个正常工作的插件:plnkr.co/edit/HX3LT9RaJD6ki3vpa1aW。由于数据存储在数组中,因此您还需要遍历数组以显示您存储的单独信息
    • 如果我错了,请纠正我,但看起来引导程序在 Angular 2 final 中发生了变化。你们知道如何调整您提供的解决方案吗?谢谢!
    • 确实,这个提供的解决方案似乎不适用于最新版本的 Angular 5。如果您尝试显示 Service cannot be used as an entry component,引导语法也已更改
    【解决方案2】:

    您需要在您的模块中添加一个全局提供程序,然后您不需要在每个组件中添加此提供程序。试试这个

    app.module.ts

    @NgModule({
        imports: [BrowserModule, FormsModule, HttpModule],
        declarations: [AppComponent, LoginComponent, InComponent],
        providers: [LoginService],
        bootstrap: [AppComponent]
    })
    

    app.component.ts

    @Component({
        moduleId: module.id,
        selector: 'app',
        templateUrl: './app.component.html'
    })
    export class AppComponent {
        constructor(public loginService: LoginService) {
    
        }
    }
    

    login.component.ts

    @Component({
        moduleId: module.id,
        selector: 'login',
        templateUrl: './login.component.html'
    })
    export class LoginComponent {
    
        constructor(public loginService: LoginService) {
    
        }
    }
    

    我希望这对你有用。

    【讨论】:

      【解决方案3】:

      除了您的需求及其解决方案之外,您还可以考虑使用 Facade + Shared Services。我这里有一个小项目:https://github.com/cmandamiento/angular-architecture-base

      【讨论】:

      • 欢迎来到 Stack Overflow。我建议你添加一个如何使用 Facade 的小例子。
      猜你喜欢
      • 2016-08-23
      • 1970-01-01
      • 2016-06-29
      • 2017-01-04
      • 2017-12-21
      • 2017-01-20
      • 2017-04-15
      • 2019-05-03
      • 2017-06-29
      相关资源
      最近更新 更多