类型为'void'的'this'上下文不能赋值给'Observable <string>'类型的方法'this'

Cap*_*das 20 angular-material angular

我在angular的自动完成示例中收到此错误.这是代码:

ngOnInit() {
      this.filteredOptions = this.myControl.valueChanges
          .pipe(
            startWith(''),
            map(val => this.filter(val))
          );
}
Run Code Online (Sandbox Code Playgroud)

错误是在startWith上生成的.我在第二个val也遇到了错误.它说:

"{}"类型的参数不能分配给"string"类型的参数

功能是:

 filter(val: string): string[] {
    return this.options.filter(option =>
      option.toLowerCase().indexOf(val.toLowerCase()) === 0);
  }
Run Code Online (Sandbox Code Playgroud)

提示: 混合控件或导入有一些东西.我不确定,但是当我创建一个新组件时,一切正常.

小智 59

我只是有同样的问题,事实证明我是从错误的目录导入map和startWith.

import {map, startWith} from "rxjs/operators";
Run Code Online (Sandbox Code Playgroud)

使用它来导入map和startWith,它应该工作和编译.

  • 同样的问题-&gt;从'rxjs / operator / delay'导入{delay}; 而不是从'rxjs / operators / delay'导入{delay}; (2认同)