typescript 错误:类型“Observable<any>”上不存在属性“pipe”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47699090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error: Property 'pipe' does not exist on type 'Observable<any>'
提问by TK123
I'm copy pasting one of the examples from here:
我正在复制粘贴这里的示例之一:
https://material.angular.io/components/autocomplete/overview
https://material.angular.io/components/autocomplete/overview
HTML:
HTML:
<form class="example-form">
<mat-form-field class="example-full-width">
<input type="text" placeholder="Pick one" aria-label="Number" matInput [formControl]="myControl" [matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let option of filteredOptions | async" [value]="option">
{{ option }}
</mat-option>
</mat-autocomplete>
</mat-form-field>
</form>
TS:
TS:
import {Component} from '@angular/core';
import {FormControl} from '@angular/forms';
import {Observable} from 'rxjs/Observable';
import {startWith} from 'rxjs/operators/startWith';
import {map} from 'rxjs/operators/map';
/**
* @title Filter autocomplete
*/
@Component({
selector: 'autocomplete-filter-example',
templateUrl: 'autocomplete-filter-example.html',
styleUrls: ['autocomplete-filter-example.css']
})
export class AutocompleteFilterExample {
myControl: FormControl = new FormControl();
options = [
'One',
'Two',
'Three'
];
filteredOptions: Observable<string[]>;
ngOnInit() {
this.filteredOptions = this.myControl.valueChanges
.pipe(
startWith(''),
map(val => this.filter(val))
);
}
filter(val: string): string[] {
return this.options.filter(option =>
option.toLowerCase().indexOf(val.toLowerCase()) === 0);
}
}
CSS:
CSS:
.example-form {
min-width: 150px;
max-width: 500px;
width: 100%;
}
.example-full-width {
width: 100%;
}
But I get this error:
但我收到此错误:
Failed to compile: Property 'pipe' does not exist on type 'Observable'.
编译失败:“Observable”类型上不存在“pipe”属性。
Any idea why?
知道为什么吗?
回答by joshvito
Make sure you are usingthe latest version of [email protected] and angular.
确保您使用的是最新版本的 [email protected] 和 angular。
https://github.com/ReactiveX/rxjs/blob/master/doc/lettable-operators.md
https://github.com/ReactiveX/rxjs/blob/master/doc/lettable-operators.md
回答by Vikas Kumar
I found issue in your code i.e
我在你的代码中发现了问题,即
myControl: FormControl = new FormControl();
Remove 'FormControl' from this line. Actually you are defining its 'Type' but this doesn't exist for 'new FormControl()', I am wondering why 'tslint' not giving error in your code. Anyway see again https://material.angular.io/components/autocomplete/overview, here angular also not using like the code you have pasted here.
从此行中删除“FormControl”。实际上,您正在定义它的“类型”,但“新 FormControl()”不存在,我想知道为什么“tslint”没有在您的代码中给出错误。无论如何再次查看https://material.angular.io/components/autocomplete/overview,这里的 angular 也不像您在此处粘贴的代码那样使用。
回答by info2ankit
Please check for the RxJS version compatibility.
As i did go through you code and found correct and working. Kindly see for the
version of RxJS should be > 5.5
请检查 RxJS 版本兼容性。
正如我确实通过你的代码,发现正确和工作。请参阅
version of RxJS should be > 5.5
回答by joshrathke
Based on your comments above it seems like you are just trying to subscribe to the value changes that occur on that FormControl. Try this:
根据您上面的评论,您似乎只是想订阅该 FormControl 上发生的值更改。试试这个:
this.filteredOptions = this.myControl.valueChanges
.subscribe(_Changes => {
console.log(_Changes);
}
Not exactly sure what your intent is here, but this should at least start spitting the change values out at you.
不完全确定您的意图是什么,但这至少应该开始向您吐出更改值。