gpt4 book ai didi

angular - 'rxjs/operators' 和 'rxjs/add/operator/' 有什么区别?

转载 作者:搜寻专家 更新时间:2023-10-30 21:21:51 25 4
gpt4 key购买 nike

import { map } from 'rxjs/operators';import 'rxjs/add/operator/map'; 有什么区别?

我在执行登录的服务方法中使用它:

// import { map } from 'rxjs/operators'; // Fails at runtime
import 'rxjs/add/operator/map'; // Works fine at runtime

public login(username: string, password: string): Observable<any> {
console.log('Sending the login credentials to obtain a token');
const credentials = { 'email': username, 'password': password };
return this.httpService.postWithHeadersInResponse(URI_LOGIN, credentials)
.map((response: HttpResponse<any>) => {
const header = response.headers.get(this.authService.getHeaderName());
const token = this.authService.extractTokenFromHeader(header);
console.log('The token from the response header: ' + token);
this.authService.setJwtTokenToLocalStorage(token);
});
}

最佳答案

不同之处在于,当您使用 rxjs/add/operator/map 时,它会更改 Observable 的原型(prototype),因此您可以使用 . (点)运算符链接:

this.httpService.postWithHeadersInResponse(URI_LOGIN, credentials)
.map(...);

但这种使用运算符的方式已被弃用。当前使用 rxjs/operators 的方式:

import { map } from 'rxjs/operators';
this.httpService.postWithHeadersInResponse(URI_LOGIN, credentials)
.pipe(map(...));

关于angular - 'rxjs/operators' 和 'rxjs/add/operator/' 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52163415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com