我正在构建一个Angular5项目,我必须使用http。我在这里使用observable,但是当我使用'
时 scope.fetchApi = Observable.of(data);
然后它给了我一个错误:
Property 'of' does not exist on type 'typeof Observable'.
我也导入了'rxjs/add/observable/of';
,但同样的错误。
我也曾尝试过import { Observable } from 'rxjs/Observable';
,但却抛出了错误:
Module '"eclipse:angular5-example/node_modules/rxjs/Observable"' has no exported member 'Observable'.
您可以看到我的代码如下:
import { Component, OnInit } from '@angular/core';
import { UserService } from './app.service';
import { FetchApi } from '../models/fetch-api.model';
import { Router } from '@angular/router';
import { Observable } from 'rxjs';
import 'rxjs/add/observer/of';
@Component({
selector: 'app-fetch-api',
templateUrl: './fetch-api.component.html',
styleUrls: ['./fetch-api.component.css']
})
//
export class FetchApiComponent implements OnInit {
fetchApi: Observable<FetchApi[]>;
constructor(private router: Router, private userService: UserService) {
}
ngOnInit() {
const scope = this;
this.userService.getUsers()
.subscribe( data => {
scope.fetchApi = Observable.of(data);
//this.fetchApi = data;
});
};
}
请详细说明。
答案 0 :(得分:1)
阅读此https://github.com/ReactiveX/rxjs/blob/master/docs_app/content/guide/v6/migration.md
导入路径已更改。
import { Observable, of } from 'rxjs';
答案 1 :(得分:0)
仅返回(data)而不是Observable.of(data)