我正在从教程(https://angular.io/tutorial/toh-pt4#inject-message-service)学习新的角度。在添加服务
之后运行应用程序时,我陷入了困境../ node_modules / rxjs / Rx和#34;'没有导出的成员'。
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.
答案 0 :(得分:60)
从您的代码中看起来您正在遵循基于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语句应如下所示
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
// or
import { of } from 'rxjs/observable/of';
检查以下链接是否有完整的更新日志和安装兼容性包rxjs-compat
的说明,以便从角度5升级到6。
请参阅此链接以供参考:https://www.academind.com/learn/javascript/rxjs-6-what-changed/