node_modules/rxjs/Rx"' has no exported member 'of'
程序员文章站
2022-05-30 12:14:08
...
按照官方文档写代码
import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';
//import { Observable } from 'rxjs/Observable';
//import 'rxjs/add/observable/of';
import {Hero} from './hero';
import {HEROS} from './mock-heros';
@Injectable()
export class HeroService {
constructor() { }
//获取英雄列表
getHeros(): Observable<Hero[]> {
return of(HEROS);
}
}
但Angular编译失败,报错
后来在rxjs帮助文档中找到解决办法;
http://reactivex.io/rxjs/manual/installation.html
正确代码如下:
import { Injectable } from '@angular/core';
//import { Observable, of } from 'rxjs';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import {Hero} from './hero';
import {HEROS} from './mock-heros';
@Injectable()
export class HeroService {
constructor() { }
//获取英雄列表
getHeros(): Observable<Hero[]> {
return Observable.of(HEROS);
}
}