【发布时间】:2018-06-18 17:22:03
【问题描述】:
我刚刚从 Angular5 更新到 6。更新后,我运行代码迁移到 rxjs6,它改变了我使用 takeWhile 的代码。现在为了订阅服务,我的代码如下所示:
this.menuService.currentMenu.pipe(takeWhile(() => this.isAlive))
.subscribe(result => {
if(result && result.name)
{
//do stuff
}
}
);
使用这个import 声明
import { takeWhile } from 'rxjs/operators';
查看 rxjs6 的文档后,现在看来这是使用 takewhile 的正确方法;但是我收到错误消息:
类型“{}”上不存在属性“名称”
看起来takewhile 操作正在剥离observable 的输入。 filter 也会发生这种情况,所以我认为 pipe 是问题所在。
当我在 stackblitz 中设置测试时,我没有收到错误,我预计这可能是由于 stackblitz 中的项目不在同一个 typescript 版本上?
这是我正在使用的版本:
_ _ ____ _ ___
/ \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ △ \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
/ ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ \_\_| |_|\__, |\__,_|_|\__,_|_| \____|_____|___|
|___/
Angular CLI: 6.0.8
Node: 10.4.1
OS: darwin x64
Angular: 6.0.5
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router
Package Version
------------------------------------------------------------
@angular-devkit/architect 0.6.8
@angular-devkit/build-angular 0.6.8
@angular-devkit/build-optimizer 0.6.8
@angular-devkit/core 0.6.8
@angular-devkit/schematics 0.6.8
@angular/cdk 6.2.1
@angular/cli 6.0.8
@angular/material 6.2.1
@angular/material-moment-adapter 6.2.1
@ngtools/webpack 6.0.8
@schematics/angular 0.6.8
@schematics/update 0.6.8
rxjs 6.2.1
typescript 2.7.2
webpack 4.8.3
编辑: 显式指定类型可以解决此问题。但我不明白为什么我必须这样做,为什么它不能保持打字。
this.menuService.currentMenu.pipe(takeWhile(() => this.isAlive))
.subscribe((result: ResultClass) => {
if(result && result.name)
{
//do stuff
}
}
);
【问题讨论】:
-
可能是一些打字稿版本问题。 subscribe(result: any) 将解决问题
-
将
: any添加到订阅并不能解决问题。 -
如果像
subscribe((result: any) => ...)这样使用any没有帮助,那么问题出在其他地方,而不是在takeWhile。 -
@martin 好的,
subscribe((result: any) => ...)工作。 (带有额外的括号)。但是为什么我需要在我的项目中指定类型,而在 Stackblitz 上它会自动从 observable 继承? -
我怀疑它是
takeWhile,因为const source = of(1).pipe(takeWhile(() => true))看到source推断为Observable<number>。我认为这取决于this.menuService.currentMenu的类型,而这不包括在问题中。
标签: angular rxjs angular6 rxjs6