angular - 收到错误 ../node_modules/rxjs/Rx"' has no export member 'of'
问题描述
我正在从教程中学习新的角度(https://angular.io/tutorial/toh-pt4#inject-message-service)。添加服务后,我在运行应用程序时遇到了这个问题
../node_modules/rxjs/Rx"' 没有导出的成员 'of'。
hero.service.ts
---------------------
import { Injectable } from '@angular/core';
// import { Observable, of } from 'rxjs';
import { Observable, of } from 'rxjs/Observable';
import { Hero } from './hero';
import { HEROES } from './mock-heroes';
import { MessageService } from './message.service';
@Injectable()
export class HeroService {
constructor(private messageService: MessageService) { }
getHeroes(): Observable<Hero[]> {
// TODO: send the message _after_ fetching the heroes
this.messageService.add('HeroService: fetched heroes');
return of(HEROES);
}
}
我的 Angular 版本和相关信息是
Angular CLI: 1.7.4
Node: 6.14.1
OS: linux x64
Angular: 5.2.10
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router
@angular/cli: 1.7.4
@angular-devkit/build-optimizer: 0.3.2
@angular-devkit/core: 0.3.2
@angular-devkit/schematics: 0.3.2
@ngtools/json-schema: 1.2.0
@ngtools/webpack: 1.10.2
@schematics/angular: 0.3.2
@schematics/package-update: 0.3.2
typescript: 2.5.3
webpack: 3.11.0
@angular/cli: 1.7.4
@angular-devkit/build-optimizer: 0.3.2
@angular-devkit/core: 0.3.2
@angular-devkit/schematics: 0.3.2
@ngtools/json-schema: 1.2.0
@ngtools/webpack: 1.10.2
@schematics/angular: 0.3.2
@schematics/package-update: 0.3.2
typescript: 2.5.
解决方案
从您的代码看来,您正在遵循基于 Angular 6 和 Rxjs 6 的 Angular 官方指南。Rxjs 中有一个重大变化,您现在必须以不同的方式导入operators
它Observable
。
在 Rxjs 6 中,导入如下所示 -
import { Observable, of } from 'rxjs'; // only need to import from rxjs
但是当您使用 Angular 5.2.x 时,很可能您仍在使用 Rxjs 5x 版本。因此,您的导入声明应如下所示
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
// or
import { of } from 'rxjs/observable/of';
检查以下链接以获取完整的更改日志和安装兼容性包rxjs-compat
以从 angular 5 升级到 6 的说明。
请参阅此链接以供参考:https ://www.academind.com/learn/javascript/rxjs-6-what-changed/
推荐阅读
- multithreading - 同一个条件变量的定时等待和无限等待?
- c# - Using CSVHelper to read a CSV file and need to extract columns to arrays
- r-markdown - Bookdown/Huskydown 中的表格,具有多种功能(引文、标题、URL、PNG 图……)
- javascript - React - TypeError:this.props.AccountId 不是函数
- pytorch - 如何在张量中复制输入通道?
- java - Apache-poi:无法在 docx 标头中添加图像
- javascript - VueJS Components inside a loop act as one
- emv - 无法通过 BP-Tools 验证密码
- java - 将变量名称永久转换为新类型
- docker - 将 docker 镜像拉到 Heroku 容器