我是Angular的新手。
我该如何解决这个问题?
我已经安装了Angular CLI: 11.0.7
和Node: 12.18.4
错误:
错误:src/app/_services/account.service.ts:19:7 - 错误TS2345: 类型“OperatorFunction<User,void>”的参数无法分配给类型“OperatorFunction<Object,void>”。 “Object”类型只能分配给极少数其他类型。您是否想使用“any”类型? 类型“Object”缺少类型“User”的以下属性:username,token
19 map((response: User) => { ~~~~~~~~~~~~~~~~~~~~~~~~~ 20 const user = response; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ... 24 } ~~~~~~~~~ 25 }) ~~~~~~~~
account.service.ts
import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; import {map} from 'rxjs/operators'; import { User } from '../_models/user'; import { ReplaySubject } from 'rxjs'; export class AccountService { baseUrl = 'https://localhost:5001/api/'; private currentUserSource = new ReplaySubject<User>(1); currentUser$ = this.currentUserSource.asObservable(); constructor(private http: HttpClient) { } login(model: any) { return this.http.post(this.baseUrl + 'account/login', model).pipe( map((response: User) => { const user = response; if (user) { localStorage.setItem('user', JSON.stringify(user)); this.currentUserSource.next(user); } }) ) } }
user.ts
export interface User{ username: string; token: string; }