Rxjs 角度6:类型“typeof Observable”上不存在属性“of”
我用的是角度6 使用rxjs:^6.0.0 错误:类型“typeof Observable”上不存在属性“of”Rxjs 角度6:类型“typeof Observable”上不存在属性“of”,rxjs,angular6,rxjs6,Rxjs,Angular6,Rxjs6,我用的是角度6 使用rxjs:^6.0.0 错误:类型“typeof Observable”上不存在属性“of” import { Injectable } from '@angular/core'; import { TranslateLoader } from '@ngx-translate/core'; import { Observable, Subject, pipe, of } from 'rxjs'; @Injectable() export class MiTranslate
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/observable/of导入
用法:
return of({
lbl_select: 'Select',
});
更新:对于没有rxjs compat的rxjs版本6,您需要从rxjs本身导入的,如@martin所述
import { of } from 'rxjs';
随着版本6的发布,RxJS改变了其内部包结构
由于RxJS 6在Observable.of中使用RxJS 5的正确且推荐的方法是: 我认为这个{of}来自于'rxjs/observable/of';只有在安装了rxjs compat软件包后才能工作。rxjs中有一些更新:它的rxjs6 只有当您的应用程序安装了rxjs compat软件包时,它才会起作用 您可以从rxjs导入: 从'rxjs'导入{可观察的} 简单地返回 因此,您的代码将是:
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',
});
}
}
解决办法是返回。。直接:
getTranslation(lang: string): Observable<any> {
return of({
lbl_select: 'Select',
});
这对我有用 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.');
});
需要注意的是,您必须将return Observable.of更改为return of
import { of } from 'rxjs';
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',
});
}
}
getTranslation(lang: string): Observable<any> {
return of({
lbl_select: 'Select',
});
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.');
});