得到错误../node_modules/rxjs/Rx"'没有导出成员“of" [英] Getting the error ../node_modules/rxjs/Rx"' has no exported member 'of'

查看:25
本文介绍了得到错误../node_modules/rxjs/Rx"'没有导出成员“of"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从教程 (https://angular.io/tutorial/toh-pt4#inject-message-service).添加服务后,我在运行应用程序时陷入困境

I'm learning new angular from the tutorial(https://angular.io/tutorial/toh-pt4#inject-message-service). I'm stuck in this while running the application after adding the Services

../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 版本和相关信息是

My Angular version and related information are

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 中有一个重大更改现在以不同的方式导入 operatorsObservable .

From your code it looks like you are following Angular official guide which is based on Angular 6 and Rxjs 6. There is a breaking change in Rxjs for which you have to import operators and Observable in a different way now .

在 Rxjs 6 中,导入如下 -

In Rxjs 6 the import is like below -

import { Observable, of } from 'rxjs'; // only need to import from rxjs

但是当您使用 Angular 5.2.x 时,您很可能仍在使用 Rxjs 5x 版本.因此,您的导入语句应如下所示

But as you are using Angular 5.2.x most probably you are still using Rxjs 5x version. Due to which your import statement should be like below

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
// or 
import { of } from 'rxjs/observable/of';

检查以下链接以获取完整的更改日志和安装兼容包 rxjs-compat 以从 angular 5 升级到 6 的说明.

Check the below link for complete changelog and instruction to install a compatibility package rxjs-compat for upgrading from angular 5 to 6.

请参阅此链接以供参考:https://www.academind.com/learn/javascript/rxjs-6-what-c​​hanged/

See this link for reference : https://www.academind.com/learn/javascript/rxjs-6-what-changed/

这篇关于得到错误../node_modules/rxjs/Rx"'没有导出成员“of"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆