【问题标题】:Why would I be unable to access a pipe method on a Subject?为什么我无法访问主题上的管道方法?
【发布时间】: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


【解决方案1】:

您的getProperty 调用未返回Subject(或任何其他Observable-like,因此其余代码无法工作。尝试仅返回pipe 的结果而不使用subscribe 调用.

【讨论】:

    猜你喜欢
    • 2020-08-26
    • 1970-01-01
    • 2012-03-19
    • 1970-01-01
    • 1970-01-01
    • 2020-02-22
    • 2020-12-11
    • 2015-01-11
    • 2014-04-06
    相关资源
    最近更新 更多