在我的 Angular-11 项目中,我在身份验证服务中有此代码:
login(data: {
username: string;password: string;rememberMe: boolean
}): Observable < any > {
const {
username,
password,
rememberMe
} = data;
const loginData: OauthInterface = {
['grant_type']: PASSPORT_CLIENT.grantType,
['client_id']: PASSPORT_CLIENT.clientId,
['client_secret']: PASSPORT_CLIENT.clientSecret,
username,
password,
scope: '',
};
// const url = `oauth/token`;
const url = `${API_URL}/oauth/token`;
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
})
};
return this.http.post < any > (url, loginData, httpOptions)
.pipe(
tap(user => rememberMe ? localStorage.setItem('currentUser', JSON.stringify(user)) : ''),
tap(user => !rememberMe ? sessionStorage.setItem('currentUser', JSON.stringify(user)) : ''),
tap(user => this.currentUserSubject.next(user)),
tap(user => this.currentUserSubject.next(user)),
this.currentUserDetails().subscribe(),
catchError(error => throwError(error))
);
}
currentUserDetails(): Observable < any > {
const url = `${API_URL}/api/user`;
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
})
};
return this.http.get < any > (url, httpOptions)
.pipe(map(user => {
this.store.dispatch({
type: '[APP STATE] set logged in user',
payload: user
});
this.currentUserSubject.next(user);
return user;
},
() => {
// Error Has been captured by interceptor
}
));
}
我收到此错误:
<块引用>“订阅”类型的参数不可分配给“OperatorFunction
突出显示这一行:
<块引用>this.currentUserDetails().subscribe(),
如何解决?
谢谢
答案 0 :(得分:0)
要快速解锁,请更改:
this.currentUserDetails().subscribe(),
到:
tap(_ => this.currentUserDetails().subscribe()),
您只能将运算符放入管道中,而不能放入函数调用中。