angular - 'Observable' 类型上不存在属性 'do''
问题描述
升级到 Angular 6.0 和 Rxjs 到 6.0 后,我收到以下编译错误:
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);
}
}
任何想法?
解决方案
问题不在于角度,而在于 rxjs。rxjs 引入了 rxjs 版本 6 的重大更改。
要让您的代码在不更改任何代码的情况下再次运行,请安装以下软件包:
npm install rxjs-compat@6 --save
然后,您应该能够编译您的项目。rxjs-compat
旨在作为临时解决方案,因此您需要更新代码库以使用新版本。
新的导入路径
您需要更新的内容:
更新导入语句
import { Observable } from "rxjs/Observable";
至
import { Observable } from "rxjs";
更新您的运营商进口自
import 'rxjs/add/operator/do'
至
import { do } from "rxjs/operators";
重命名运算符
由于与 JavaScript 保留字的名称冲突,一些运算符也已重命名。他们是
do
=>tap
catch
=>catchError
switch
=>switchAll
finally
=>finalize
没有运算符链接
你也不能再链接你的运营商了,你需要使用pipe
运营商,例如
// 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);
推荐阅读
- visual-studio - 如何取消 VisualStudio 中项目的重建而无需再次构建?
- java - 使用时间戳签署 XML 文档
- spring-cloud-stream - Spring Cloud kafka Stream Binder transactionIdPrefix 性能命中
- ruby-on-rails - 从初始化程序调用 ActionMailer::Base.mail 方法时“不可用”
- java - 在android改造中使用mutlipart注释时如何发送对象
- python - 使用 pandas 将文件连接成 2 个数据帧
- reactjs - 页脚文本未呈现
- github - ValueError:无法将大小为 30200 的数组重新整形为形状 (3772,8)
- azure-data-explorer - 如何在for循环中调用表格函数
- python-3.x - Python 请求 - 获取请求时发布 SSL 错误