简体   繁体   English

在Angular2中共享可观察/主题

[英]Sharing an observable/subject in Angular2

EDIT: For what it's worth, I've learned that the SharedService is getting initialized twice. 编辑:对于它的价值,我了解到SharedService初始化了两次。 I suspect I'm working with separate instances, which is why the .subscribe() is limited only to the initiator. 我怀疑我正在使用单独的实例,这就是为什么.subscribe()仅限于启动器的原因。 I don't know how to correct this... I thought everything was a singleton in Angular2? 我不知道该如何纠正...我以为Angular2中的一切都是单例?

I have two components. 我有两个组成部分。 I want either component to be able to POST through a service, and have both components receive the response. 我希望任何一个组件都能通过服务进行POST,并使两个组件都收到响应。

The two components are pretty basic and essentially identical for the demonstration of this issue. 这两个组件非常基本,并且在演示此问题时基本上相同。 Nothing really big to see here but here they are just in case: 这里没什么大不了的,但是在这里以防万一:

Foo Component Foo组件

@Component({
    selector: "foo",
    providers: [SharedService]
})

export class FooComponent {
    constructor(private _api:API, private _service:SharedService) {
        this._service.data$.subscribe(
            response => console.log("FOO!", response)
            // error not required here since the service handles it for all dependents.
        );
    }

    doSomething(values) {
        this._service.post(values);
    }
}

Bar Component 条形组件

@Component({
    selector: "bar"
})

export class BarComponent {
    constructor(private _api:API, private _service:SharedService) {
        this._service.data$.subscribe(
            response => console.log("BAR!", response)
            // error not required here since the service handles it for all dependents.
        );
    }

    doSomething(values) {
        this._service.post(values);
    }
}

The SharedService is where I've been trying to get things right. 我一直在尝试SharedService来解决问题。

@Injectable()
export class SharedService {
    private _observer: Observer<Response>;
    public data$;

    // i wrapped Http in an API service.  it does the following:
    // 1.  convenience methods like .map() for the observables
    // 2.  adding headers to every request so that i don't need to do that for every service
    // 3.  takes the first parameter e.g. "login" and converts it to a full endpoint
    // 4.  converts the "values" object in to a body string

    constructor(private _api: API) {
        this.data$ = new Observerable(observer = this._observer = observer).share();
    }

    post(values:IValues) {
        this._api.post("foobar", values).subscribe(
            response => this._observer.next(response),
            error => console.log("foobar failure", error);
        );
    }
}

No matter what I try, I get only the success condition of the invoking component. 无论我尝试什么,都只会得到调用组件的成功条件。 So if I trigger the service from "Foo", I get "FOO!" 因此,如果我从“ Foo”触发服务,则会收到“ FOO!”。 and if I invoke it from "Bar", I get "BAR!". 如果从“ Bar”调用它,则会得到“ BAR!”。

Ideally, I could invoke it from "Foo" and receive back "FOO!" 理想情况下,我可以从“ Foo”中调用它并接收回“ FOO!” and "BAR!". 和“ BAR!”。 The order of these responses doesn't really matter. 这些响应的顺序并不重要。

Problem was that I had this and didn't realize it: 问题是我有这个但没有意识到:

@Component({
    selector: "foo",
    providers: [SharedService] // only existed on one of them
})

@Component({
    selector: "bar"
})

Which created two instances. 其中创建了两个实例。 Working code is just: 工作代码只是:

@Component({
    selector: "foo"
})

@Component({
    selector: "bar"
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM