angular2类型&#39;主题&lt; {}&gt;&#39;不能分配给&#39;()=&gt;受试者<编号>&#39;

时间:2016-06-30 02:39:23

标签: typescript angular rxjs multicast

我使用angular2 rxjs来测试它的多播功能,这是我的代码:

var source = Observable.from([1, 2, 3]);
var subject = new Subject();
var multicasted = source.multicast(subject);

// These are, under the hood, `subject.subscribe({...})`:
multicasted.subscribe({
    next: (v) => console.log('observerA: ' + v)
});
multicasted.subscribe({
    next: (v) => console.log('observerB: ' + v)
});

// This is, under the hood, `source.subscribe(subject)`:
multicasted.connect();

我收到了错误信息:

类型&#39;主题&lt; {}&gt;&#39;不能分配给

类型的参数
'Subject<number> | (() => Subject<number>)'.
        Type 'Subject<{}>' is not assignable to type '() => Subject<number>'.
        Type 'Subject<{}>' provides no match for the signature '(): Subject<number>'

1 个答案:

答案 0 :(得分:6)

像@yurzui说的那样,你需要定义泛型主题的类型

var subject = new Subject<number>();