角度 6 - rxjs 属性'map'在类型 'observable' 上不存在



升级到Angular 6时出现此错误。我看到了使用.pipe((的文档,但当有多个.map((时,我不知道如何使用pipe,如下所示。需要你的帮助。。。

import {Injectable} from '@angular/core';
import {Http, Headers} from '@angular/http';
import 'rxjs/add/operator/map';
import {TOKEN_AUTH_PASSWORD, TOKEN_AUTH_USERNAME} from '../services/auth.constant';
@Injectable()
export class AuthenticationService {
static AUTH_TOKEN = '/oauth/token';
constructor(private http: Http) {
}
login(username: string, password: string) {
const body = `username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}&grant_type=password`;
const headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
headers.append('Authorization', 'Basic ' + btoa(TOKEN_AUTH_USERNAME + ':' + TOKEN_AUTH_PASSWORD));
return this.http.post(AuthenticationService.AUTH_TOKEN, body, {headers})
//.map(res => res.json())
.pipe(map((res: any) => {
if (res.access_token) {
return res.access_token;
}
return null;
}));
}
}

我理解在有一个.map的情况下使用.pipe,如下所示,但我不知道如何在使用多个.map时使用pipe((。

.pipe(map(data => {})).subscribe(result => {
console.log(result);
});

要将管道与多个运算符一起使用,应使用逗号:

return this.http.post(AuthenticationService.AUTH_TOKEN, body, {headers})
.pipe(
map(res => res.json()),
map((res: any) => {
if (res.access_token) {
return res.access_token;
}
return null;
})
);

但在您的情况下不需要使用CCD_ 1。

在以前的版本中,导入为import 'rxjs/add/operators/map'

在Angular 6中,它应该是:import { map } from 'rxjs/operators';

顺便说一句,如果您期望取消效果,您应该使用switchMap而不是map。阅读更多

相关内容

最新更新