【发布时间】:2019-08-07 17:41:57
【问题描述】:
当将 Subject 对象传递给我创建的方法时,我相信主题 foo$ 如下所示,由于 Subject 的类定义扩展了 Observable,因此应该可以访问 pipe() 方法,但我没有在运行时访问 pipe() 方法。代码编译正常,在 VSCode 中没有问题。
我曾尝试在 .pipe() 之前使用 foo$.asObservable(),但类似的问题发生在我在运行时被告知“foo$.asObservable 不是函数”的地方。我还尝试将 foo$ 转换为 Observable 或 Subject ,因为当我在此方法中记录 foo$ 的类型时,仅显示“对象”。
当在管道之前使用 of(foo$) 来创建 observable 时,这确实有效,但我不再可以访问下面代码中需要的 _.change 或 _.list 值。我曾尝试使用此实现将这些值存储在其他地方,但我无法提出任何建议。
ngOnInit() {
super.init();
const config = super.getCurrentConfig();
this.model.updateProperties([['graphicName', '/' + config.graphicName]]);
this.model.updateProperties([['inputs', config.inputs ? config.inputs : []], ['identifier', config.identifer ? config.identifier : '']]);
this.systemMessageBusService.push(new SystemMessage(SystemMessageAction.GET_THE_FOO, name));
this.subscriptions.push(
this.model
.getProperty('foo$')
.pipe(filterNotNulls(), distinctUntilChanged())
.subscribe(_ => this.subscriptionUpdated(_))
);
}
private subscriptionUpdated(foo$: Subject<ListValue<BarFoo>>) {
this.model.updateProperty('foosSubscription', foo$
.pipe(filterNotNulls())
.subscribe(_ => {
if (!_.change) {
this.myClass.doThis(_.list);
} else {
this.myClass.doThat(_.change);
}
})
);
}
updateProperty<K extends keyof T>(propertyName: K, value: any) {
const currentValue: T = Object.assign({}, this.getCurrentValue());
currentValue[propertyName] = value;
this.update(currentValue);
}
预期结果:我能够在我的主题上使用 .pipe 方法。
实际结果:“foo$.pipe 不是函数”
【问题讨论】:
-
尝试在管道之前调用 asObservable 方法
-
@EduardoVargas 我也试过了,我收到了类似的问题,“错误:TypeError ... foo$.asObservable 不是函数”
-
请您发布 updateProperty 吗?
-
您将
Subscription对象作为value传递给updateProperty()。这是您的意图吗,因为该函数中的代码似乎与订阅没有任何关系。 -
一个体面的 IDE 已经为你的项目建立了索引会强调这是一个错误。
标签: javascript angular typescript rxjs observable