代码之家  ›  专栏  ›  技术社区  ›  N Sharma

类型“type of observable”上不存在“of”属性

  •  1
  • N Sharma  · 技术社区  · 8 年前

    我正在构建一个需要使用HTTP的Angular5项目。我在这里使用Observable,但是当我使用“of”时

     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;
          });
      };
    
    
    
    }
    

    请照一下。

    2 回复  |  直到 7 年前
        1
  •  1
  •   Ajay S    8 年前

    读这个 https://github.com/ReactiveX/rxjs/blob/master/docs_app/content/guide/v6/migration.md

    导入路径已更改。

    import { Observable, of } from 'rxjs';
    
        2
  •  0
  •   Pranav    7 年前

    只返回(数据)而不可观察(数据)