Error: src/app/app.service.ts:20:11 - error TS7053: Element implicitly has an 'any' type because expression of type '"name"' can't be used to index type 'Object'.
Property 'name' does not exist on type 'Object'.
20 if (response['name']) {
~~~~~~~~~~~~~~~~
遇到上面的错误我们只需要将reponse的类型设置为any就可以了
authenticate(credentials: { username: string; password: string; }, callback: () => void) {
const headers = new HttpHeaders(credentials ? {
authorization: 'Basic ' + btoa(`${credentials.username}:${credentials.password}`)
} : {});
this.http.get('user', {headers: headers}).subscribe((response: any) => {
if (response['name']) {
this.authenticated = true;
} else {
this.authenticated = false;
}
return callback && callback();
});
}