要解决Angular Service变量在组件中没有更新的问题,可以尝试以下解决方法:
constructor(private myService: MyService) {}
updateVariable(newValue: any) {
this.myService.variable = newValue;
}
export class MyService {
private _variable: any;
get variable(): any {
return this._variable;
}
set variable(value: any) {
this._variable = value;
}
}
fetchData() {
this.http.get('api/data').subscribe((response) => {
this.myService.variable = response;
});
}
通过以上方法,可以确保在组件中正确更新Service变量的值,并且在需要的时候获取到最新的值。