角度6:'属性'在类型' typeof Observable'中不存在

时间:2018-06-15 07:12:09

标签: rxjs angular6 rxjs6

我正在使用 Angular 6 使用" rxjs":" ^ 6.0.0",

错误:属性''类型' typeof Observable'。

上不存在
import { Injectable } from '@angular/core';
import { TranslateLoader } from '@ngx-translate/core';
import { Observable, Subject, pipe, of } from 'rxjs';


@Injectable()
export class MiTranslateLoaderService implements TranslateLoader {

  getTranslation(lang: string): Observable<any> {
    return Observable.of({
      lbl_select: 'Select',
    });
  }
}

6 个答案:

答案 0 :(得分:14)

从RxJS 6开始,使用of()Observable.of()中的RxJS 5)的正确和推荐方法是:

import { of } from 'rxjs';

我认为只有安装了import { of } from 'rxjs/observable/of';软件包时,此rxjs-compat才有效。

答案 1 :(得分:4)

rxjs中有一些更新:(其rxjs6)

import axios from 'axios';

import {SET_UPDATE_SCHEDULES} from './types';

let requestData;

const getAttributes = (updateSchedules, callback) => {
  let promises = [];
  updateSchedules.map((updateSchedule) => {
    promises.push(axios.get(updateSchedule.status.href, requestData).then(res => updateSchedule.statusName = res.data.name));

    promises.push(axios.get(updateSchedule.edge_device.href, requestData).then( res => updateSchedule.edgeDeviceName = res.data.serial_number))
  })


  axios.all(promises)
  .then(axios.spread(function(...promises){
    console.log("requests")
    promises.map((promise) => console.log(promise))
  }))
  .then(callback(updateSchedules)) //Here it gets fired before I receive the data
}

export const getUpdateSchedules = (selectedDB,userData) => dispatch =>{

  requestData = userData;

  axios.get(selectedDB+'somelink/id', requestData)
  .then(res => getAttributes(res.data.update_schedules,function(updateSchedules){
    console.log("update")
  }
))

仅当您的应用程序安装了 import { of } from 'rxjs'; 软件包

时,此方法才有效

您可以从rxjs-compat导入of

rxjs

只需返回 import { Observable,of } from 'rxjs';

of()

所以您的代码将是:

 return of({
      lbl_select: 'Select',
    });

答案 2 :(得分:2)

这对我有用。

Angular CLI 6.0.8

RxJS 6.2.2

import {of} from 'rxjs/index';


this.dataService.currentData

    .pipe(takeUntil(this.destroy$))
    .pipe(switchMap((myData:MyDataType) =>
      of(this.anotherService.get(myData._id))))
    .pipe(map((response) => {
         if(response instanceof Error) {
            console.log('error:');
            console.dir(response);
         }
         return response;
    }))
    .subscribe((data:any) => {
       doStuff(data);
      },
      response => {
        console.log('response error');
        console.log(response)
      },
      () => {
        console.log('response complete.');


      });

答案 3 :(得分:0)

您需要从of

导入rxjs/observable/of
import { of } from "rxjs/observable/of";

用法:

return of({
  lbl_select: 'Select',
});

更新:对于没有rxjs-compat的 rxjs版本6 ,您需要从of本身导入rxjs,如@martin所述。

import { of } from 'rxjs';

Migration guide to rxjs6

答案 4 :(得分:0)

随着版本6的发布, RxJS 更改了其内部包结构

https://www.academind.com/learn/javascript/rxjs-6-what-changed/#import-statement-update-path

import 'rxjs/add/observable/of';
// or 
import { of } from 'rxjs/observable/of';

答案 5 :(得分:0)

解决方案是直接返回(..):

getTranslation(lang: string): Observable<any> {
    return of({
      lbl_select: 'Select',
    });