路由角时堆栈订阅
我有一种调用可观察服务的方法。我在导航时订阅并将其销毁,但是当我导航回到方法时
注意:Angular CLI:版本13.0.2 | NPM 8.1.0
//home component
public workList:workModel[] = [];
serviceSubscription:Subscription;
constructor(
public workService: WorkServices
) {
this.serviceSubscription = this.getWorkList();
}
ngOnInit(): void {
//this.serviceSubscription = this.getWorkList();
}
ngOnDestroy(): void {
this.serviceSubscription.unsubscribe();
}
getWorkList(){
this.workList = [];
return this.workService.getWorks().subscribe((resp:Array<workModel>) => {
this.workList = resp.reverse();
console.log("work list ->", this.workList);
})
}
//**************************************************************
//service
@Injectable({
providedIn: 'root'
})
export class WorkServices {
private URL = "http://localhost:3000/work";
private works:workModel[];
constructor(
public http:HttpClient
){
this.works = new Array<workModel>();
}
getWorks(){
return this.http.get(this.URL).pipe(map((resp:any) => {
resp.forEach((w:workModel) => {
this.works.push(new workModel(
w.description,
w.client,
w.employees,
w.materials,
w.isFinished,
w.cost,
w.id
))
})
return this.works;
}));
}
/*
Console
work list -> Array [ {…}, {…} ] // load page
work list -> Array(4) [ {…}, {…}, {…}, {…} ] // Navigate and back first time
work list -> Array(6) [ {…}, {…}, {…}, {…}, {…}, {…} ] // Navigate and back second time
*/
I have a method that calls an Observable service. I subscribe it and destroy it when navigate, but when I navigate back my method stack the results
Note: Angular CLI: Version 13.0.2 | npm 8.1.0
//home component
public workList:workModel[] = [];
serviceSubscription:Subscription;
constructor(
public workService: WorkServices
) {
this.serviceSubscription = this.getWorkList();
}
ngOnInit(): void {
//this.serviceSubscription = this.getWorkList();
}
ngOnDestroy(): void {
this.serviceSubscription.unsubscribe();
}
getWorkList(){
this.workList = [];
return this.workService.getWorks().subscribe((resp:Array<workModel>) => {
this.workList = resp.reverse();
console.log("work list ->", this.workList);
})
}
//**************************************************************
//service
@Injectable({
providedIn: 'root'
})
export class WorkServices {
private URL = "http://localhost:3000/work";
private works:workModel[];
constructor(
public http:HttpClient
){
this.works = new Array<workModel>();
}
getWorks(){
return this.http.get(this.URL).pipe(map((resp:any) => {
resp.forEach((w:workModel) => {
this.works.push(new workModel(
w.description,
w.client,
w.employees,
w.materials,
w.isFinished,
w.cost,
w.id
))
})
return this.works;
}));
}
/*
Console
work list -> Array [ {…}, {…} ] // load page
work list -> Array(4) [ {…}, {…}, {…}, {…} ] // Navigate and back first time
work list -> Array(6) [ {…}, {…}, {…}, {…}, {…}, {…} ] // Navigate and back second time
*/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
显然,结果在您的服务中,
works
数组中。如果不需要此行为,则可以简单地从服务中删除Works
属性,然后从getWorks
方法中删除其使用情况:Apparently the results are cached in your service, in the
works
array. If this behavior is not desired, then you can simply remove theworks
property from the service and remove the usage of it from thegetWorks
method:我看不到您破坏可观察到的可观察,工程阵列正在使用,服务也没有被摧毁。因此它保留数据。 有三种方法可以解决此
清晰的作品数组。
在组件的提供商数组中加入服务,因此服务实例与组件破坏了。
每次称为getWorks时重新引导作品数组。
该解决方案完全取决于您的用户酶。理想情况下,在您有特定方案之前,不应在服务中管理数组。
i don't see you destroying the observable, the works array is in service, and service is not destroyed. so it keeps the data. there are three ways to solve this
Clear works array when component is destroyed.
include service in provider array of component so service instance destroys with component.
Reinitialize works array everytime getWorks is called.
The solution depends totally on your usecase. ideally works array shouldn't be managed in service until you have a particular scenario.