首页 > 解决方案 > 收到错误 ../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.

标签: angularrxjsangular5

解决方案


从您的代码看来,您正在遵循基于 Angular 6 和 Rxjs 6 的 Angular 官方指南。Rxjs 中有一个重大变化,您现在必须以不同的方式导入operatorsObservable

在 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-c​​hanged/


推荐阅读