收到错误 ../node_modules/rxjs/Rx"' 没有导出成员 'of'

2024-05-19

我正在从教程中学习新的角度(https://angular.io/tutorial/toh-pt4#inject-message-service 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 and 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-c​​hanged/ https://www.academind.com/learn/javascript/rxjs-6-what-changed/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

收到错误 ../node_modules/rxjs/Rx"' 没有导出成员 'of' 的相关文章

随机推荐