角度-rxjs_Observable__WEBPACK_IMPORTED_MODULE_2 __。Observable.of不是一个函数

时间:2018-10-11 07:34:48

标签: angular

import { Injectable } from '@angular/core';
import 'rxjs/add/observable/of';
import { Observable } from 'rxjs/Observable';
import { AppSettings } from "./appsettings";
@Injectable()
export class AppSettingsService {
    getSettings(): Observable<AppSettings> {
        let settings = new AppSettings();
        return Observable.of<AppSettings>(settings);
    }
}

上面是我的angular5代码,但是我遇到了问题。请参阅控制台错误图片。

if your have any idea about this issue please share with me.

1 个答案:

答案 0 :(得分:1)

该错误是因为从版本6开始,rxjs的软件包结构已更改。以下代码适用于Angular 6:

import { Injectable } from '@angular/core';
import { Observable , of} from 'rxjs';
import { AppSettings } from "./appsettings";

@Injectable({
  providedIn: 'root'
})
export class AppSettingsService {
    getSettings(): Observable<AppSettings> {
        let settings = new AppSettings();
        return of<AppSettings>(settings);
    }
}