我有这个本地服务:
@Injectable()
export class LocalService {
private messageSource = new BehaviorSubject(null);
currentMessage = this.messageSource.asObservable();
constructor() { }
changeMessage(message: any) {
this.messageSource.next(message)
}
}
然后,当我在组件中调用它的方法时,就像这样
submitReview(){
let credentials = {
author: this.userName,
review: this.userReview.value,
movie: this.id
}
this.localService.changeMessage(credentials)
}
我收到此错误Argument of type '{ author: string; review: any; movie: string; }' is not assignable to parameter of type 'string'.
我不理解,因为消息类型设置为任何...我在做什么错?
尚不清楚,但可能是localService与应用程序中某处的localStorage交互。 localStorage通过类型字符串获取/设置。尝试对您的有效负载进行字符串化:
this.localService.changeMessage(JSON.stringify(credentials))
或在进行下一步之前在localService中进行字符串化:
this.messageSource.next(JSON.stringify(message))
或最终找到在应用程序中与localStorage实际交互的位置,并在那里进行字符串化。
希望有帮助!