类型"Observable< IProduct []>"上不存在属性"do" [英] Property 'do' does not exist on type 'Observable<IProduct[]>'
问题描述
升级到Angular 6.0和Rxjs到6.0后,我收到以下编译错误:
After upgrading to Angular 6.0 and Rxjs to 6.0 I receive the following compilation error:
Property 'do' does not exist on type 'Observable'.
这是代码:
import { Observable, of } from 'rxjs';
import 'rxjs/add/operator/do';
import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/catch';
import { IProduct } from './product';
@Injectable()
export class ProductService {
constructor(
private product: IProduct)
{
}
getProduct = () => {
return product.products
// error on next line
.do(data => console.log('All:' + JSON.stringify(data)))
.catch(this.handleError);
}
private handleError(err: HttpErrorResponse) {
console.log(err.message);
return Observable.throw(err.message);
}
}
有什么主意吗?
推荐答案
问题不是与angular有关,而与rxjs有关. rxjs引入了对rxjs版本6的重大更改.
The problem is not with angular but with rxjs. rxjs introduced breaking changes from rxjs version 6.
要在不更改任何代码的情况下再次使代码工作,请安装以下软件包:
To get your code working again without changing any of your code install the following package:
npm install rxjs-compat@6 --save
然后,您应该能够编译您的项目. rxjs-compat
只是暂时的解决方案,因此您需要更新代码库才能使用新版本.
You should then be able to compile your project. rxjs-compat
is meant to be a temporarily solution so you need to update your codebase to work with the new version.
您需要更新的内容
-
更新来自
Update import statements from
import { Observable } from "rxjs/Observable";
至
import { Observable } from "rxjs";
从以下位置更新操作员导入
Update your operator imports from
import 'rxjs/add/operator/do'
至
import { do } from "rxjs/operators";
重命名运算符
由于与JavaScript保留字的名称冲突,一些运算符也已重命名.他们是
Renamed Operators
Some operators have also been renamed due to name collisions with JavaScript reserved words. They are
-
do
=>tap
catch
=> catchError
switch
=> switchAll
finally
=> finalize
无操作员链接
您也就无法再链接您的运算符了,例如,您需要使用pipe
运算符
No Operator Chaining
You also then can't chain your operators anymore you need to use the pipe
operator e.g.
// an operator chain
source
.map(x => x + x)
.mergeMap(n => of(n + 1, n + 2)
.filter(x => x % 1 == 0)
.scan((acc, x) => acc + x, 0)
)
.catch(err => of('error found'))
.subscribe(printResult);
// must be updated to a pipe flow
source.pipe(
map(x => x + x),
mergeMap(n => of(n + 1, n + 2).pipe(
filter(x => x % 1 == 0),
scan((acc, x) => acc + x, 0),
)),
catchError(err => of('error found')),
).subscribe(printResult);
这篇关于类型"Observable< IProduct []>"上不存在属性"do"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!