Angular 6 个管道操作从 observable 中移除类型
Angular 6 pipe operations remove typing from observable
我刚从 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
的正确方法;但是我收到错误消息:
Property 'name' does not exist on type '{}'
看起来 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
}
}
);
似乎是由于我将 node
/npm
安装更改为由 nvm
处理,同时使用旧版本的 Visual Studio代码.
更新到新的 Visual Studio 代码 1.24.1 后,问题就消失了。
完整解释:
我认为旧版本的 Visual Studio 代码 不知道在哪里可以找到 npm
安装。我收到一个错误(不是马上,是在更新到 nvm
后大约一天)说了一些关于 npm
和 Automatic Type Acquisition
的信息,并找到了 this article 如何解决这个问题.我可以通过终端正常使用 npm
,它似乎一直在引起这个 Automatic Type Acquisition
问题(这是我的猜测)。
我尝试使用terminal
中的which npm
命令找到安装路径,并按照文章的指示将其放入设置文件中。它似乎没有解决问题。这可能是因为我使用了不正确的路径,或者我可能忘记重新启动 Visual Studio Code 以使设置生效(不过我想我确实重新启动了),要么我决定恢复我的设置文件并更新 Visual Studio Code 以查看新版本是否可以与 nvm
.
兼容
所以我的猜测是新的 Visual Studio 代码 知道 nvm
并且可以找到 npm
安装。
有趣的是,如果我在observable
上直接subscribe
,它可以正确地保持输入。也许升级到新的 rxjs
是问题的一部分以及其他两件事。
我刚从 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
的正确方法;但是我收到错误消息:
Property 'name' does not exist on type '{}'
看起来 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
}
}
);
似乎是由于我将 node
/npm
安装更改为由 nvm
处理,同时使用旧版本的 Visual Studio代码.
更新到新的 Visual Studio 代码 1.24.1 后,问题就消失了。
完整解释:
我认为旧版本的 Visual Studio 代码 不知道在哪里可以找到 npm
安装。我收到一个错误(不是马上,是在更新到 nvm
后大约一天)说了一些关于 npm
和 Automatic Type Acquisition
的信息,并找到了 this article 如何解决这个问题.我可以通过终端正常使用 npm
,它似乎一直在引起这个 Automatic Type Acquisition
问题(这是我的猜测)。
我尝试使用terminal
中的which npm
命令找到安装路径,并按照文章的指示将其放入设置文件中。它似乎没有解决问题。这可能是因为我使用了不正确的路径,或者我可能忘记重新启动 Visual Studio Code 以使设置生效(不过我想我确实重新启动了),要么我决定恢复我的设置文件并更新 Visual Studio Code 以查看新版本是否可以与 nvm
.
所以我的猜测是新的 Visual Studio 代码 知道 nvm
并且可以找到 npm
安装。
有趣的是,如果我在observable
上直接subscribe
,它可以正确地保持输入。也许升级到新的 rxjs
是问题的一部分以及其他两件事。