角6“的”类型上不存在“的typeof可观测”物业

问题描述 投票:7回答:6

我采用了棱角分明6使用“rxjs”:“^ 6.0.0”

ERROR:“的”上不存在型“的typeof可观测”属性。

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',
    });
  }
}
rxjs angular6 rxjs6
6个回答
15
投票

由于RxJS 6使用of()(RxJS 5 Observable.of())的正确和推荐的方法是这样的:

import { of } from 'rxjs';

我想,当你有安装包import { of } from 'rxjs/observable/of';rxjs-compat才有效。


4
投票

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

import { of } from 'rxjs';

当你的应用程序有rxjs-compat包来安装它只会工作

您可以从of导入rxjs

import { Observable,of } from 'rxjs';

并简单地返回of()

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

所以,你的代码将是:

import { Injectable } from '@angular/core';
import { TranslateLoader } from '@ngx-translate/core';
import { Observable, of } from 'rxjs';


@Injectable()
export class MiTranslateLoaderService implements TranslateLoader {

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

2
投票

这是为我工作。

角CLI 6.0.8

6.2.2 RxJS

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.');


      });

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';

0
投票

你需要从of进口rxjs/observable/of

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

用法:

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

更新:对于rxjs 6版本没有rxjs-compat的,你需要通过@马丁提到从of本身导入rxjs

import { of } from 'rxjs';

Migration guide to rxjs6


0
投票

解决的办法是直接返回的(..):

getTranslation(lang: string): Observable<any> {
    return of({
      lbl_select: 'Select',
    });
© www.soinside.com 2019 - 2024. All rights reserved.