我试图从Angular调用一个API,但得到这个错误:
属性“map”在类型“Observable<Response>”上不存在
这个类似问题的答案并没有解决我的问题:Angular 2 beta版。17:属性“map”在类型“Observable<Response>”上不存在。
我使用的是Angular 2.0.0-beta.17。
我试图从Angular调用一个API,但得到这个错误:
属性“map”在类型“Observable<Response>”上不存在
这个类似问题的答案并没有解决我的问题:Angular 2 beta版。17:属性“map”在类型“Observable<Response>”上不存在。
我使用的是Angular 2.0.0-beta.17。
当前回答
感谢https://github.com/nagamallabhanu
https://github.com/webmaxru/pwa-workshop-angular/issues/2#issuecomment-395024755
封装
管(地图(…))
工作
import { map } from 'rxjs/operators';
courseRef: AngularFireList<any>;
courses$: Observable<any[]>;
this.courseRef = db.list('/tags');
this.courses$ = this.courseRef.snapshotChanges()
.pipe(map(changes => {
return changes.map(c => ({ key: c.payload.key, ...c.payload.val()
}));
}));
this.courses$.subscribe(res=>{
console.log(res)
})
其他回答
在Angular v10中。X和RXJS v6.x
第一次导入我的服务地图顶部,
Import {map} from 'rxjs/operators';
然后我用这样的地图
return this.http.get<return type>(URL)
.pipe(map(x => {
// here return your pattern
return x.foo;
}));
我也曾犯过同样的错误。一个对我有效的解决方案是在您的服务中添加以下行。Ts文件而不是导入'rxjs/add/operator/map':
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
举个例子,我的app。service。ts在调试之后,
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
@Injectable()
export class AppService {
constructor(private http: HttpClient) {}
getData(): Observable<any> {
return this.http.get('https://samples.openweathermap.org/data/2.5/history/city?q=Warren,OH&appid=b6907d289e10d714a6e88b30761fae22')
.pipe(map(result => result));
}
}
只需在项目的VS Code终端中写入此命令并重新启动项目。
npm install rxjs-compat
你需要通过添加这个来导入map操作符:
import 'rxjs/add/operator/map';
角9:
forkJoin([
this.http.get().pipe(
catchError((error) => {
return of([]);
})
),
this.http.get().pipe(
catchError((error) => {
return of([]);
})
),
感谢https://github.com/nagamallabhanu
https://github.com/webmaxru/pwa-workshop-angular/issues/2#issuecomment-395024755
封装
管(地图(…))
工作
import { map } from 'rxjs/operators';
courseRef: AngularFireList<any>;
courses$: Observable<any[]>;
this.courseRef = db.list('/tags');
this.courses$ = this.courseRef.snapshotChanges()
.pipe(map(changes => {
return changes.map(c => ({ key: c.payload.key, ...c.payload.val()
}));
}));
this.courses$.subscribe(res=>{
console.log(res)
})