问题
I'm trying to make components send messages via a shared service, like in a cookbook's "Parent and children communicate via a service" recipe. I have a Sender
component that calls a service's method, in which an event is fired. And there's a Receiver
component, which's just listens to events.
The problem is the Receiver
doesn't get an event. Here's the code:
import {bootstrap} from 'angular2/platform/browser'
import {Injectable, Component} from 'angular2/core';
import 'rxjs/Rx';
import {Subject} from 'rxjs/Subject';
@Injectable()
export class TestService {
private _subject = new Subject<any>();
event$ = this._subject.asObservable();
doFoo() {
console.log('Doing foo hard...')
this._subject.next('hey');
}
}
@Component({
selector: 'receiver-component',
template: 'Value is {{ value }}',
providers: [
TestService
],
})
export class ReceiverComponent {
private value: number = 'nope';
constructor(private service: TestService) {
this.service.event$.subscribe(data => this.value = data)
}
}
@Component({
selector: 'sender-component',
template: '<button (click)="fireEvent()">Click me</button>',
providers: [
TestService
],
})
export class SenderComponent {
constructor (private service: TestService) {}
fireEvent() {
this.service.doFoo()
}
}
bootstrap(SenderComponent);
bootstrap(ReceiverComponent);
When I click the button, I see the debug message from TestService.doFoo()
, so it gets executed. But the event message just doesn't get passed. Why?
Update: I'm using angular2@2.0.0-beta.7
and rxjs@5.0.0-beta.2
.
回答1:
This it isn't a shared service. Each component gets it's own instance.
If you add the service to the providers
list of the component, each component will get a new instance.
If you run bootstrap()
twice, you create two distinct Angular applications which don't share anything. See the last lines of the code how to establish the connection anyway:
import {bootstrap} from 'angular2/platform/browser'
import {Injectable, Component, provide} from 'angular2/core';
import 'rxjs/Rx';
import {Subject} from 'rxjs/Subject';
@Injectable()
export class TestService {
private _subject = new Subject<any>();
event$ = this._subject.asObservable();
doFoo() {
console.log('Doing foo hard...')
this._subject.next('hey');
}
}
@Component({
selector: 'receiver-component',
template: 'Value is {{ value }}',
// providers: [
// TestService
// ],
})
export class ReceiverComponent {
private value: number = 'nope';
constructor(private service: TestService) {
this.service.event$.subscribe(data => this.value = data)
}
}
@Component({
selector: 'sender-component',
template: '<button (click)="fireEvent()">Click me</button>',
// providers: [
// TestService
// ],
})
export class SenderComponent {
constructor (private service: TestService) {}
fireEvent() {
this.service.doFoo()
}
}
sharedService = new TestService();
bootstrap(SenderComponent, [provide(TestService, {useValue: sharedService})]);
bootstrap(ReceiverComponent, [provide(TestService, {useValue: sharedService})]);
来源:https://stackoverflow.com/questions/35766631/angular2-components-fail-to-communicate-via-observable-in-a-service