路由角时堆栈订阅

发布于 2025-01-27 04:21:05 字数 1881 浏览 2 评论 0原文

我有一种调用可观察服务的方法。我在导航时订阅并将其销毁,但是当我导航回到方法时

注意: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

擦肩而过的背影 2025-02-03 04:21:05

显然,结果在您的服务中,works数组中。如果不需要此行为,则可以简单地从服务中删除Works属性,然后从getWorks方法中删除其使用情况:

getWorks() {
  return this.http.get(this.URL).pipe(
    map((resp: any) => resp.map((w: workModel) =>
      new workModel(
        w.description,
        w.client,
        w.employees,
        w.materials,
        w.isFinished,
        w.cost,
        w.id
      ))
    )
  );
}

Apparently the results are cached in your service, in the works array. If this behavior is not desired, then you can simply remove the works property from the service and remove the usage of it from the getWorks method:

getWorks() {
  return this.http.get(this.URL).pipe(
    map((resp: any) => resp.map((w: workModel) =>
      new workModel(
        w.description,
        w.client,
        w.employees,
        w.materials,
        w.isFinished,
        w.cost,
        w.id
      ))
    )
  );
}
我乃一代侩神 2025-02-03 04:21:05

我看不到您破坏可观察到的可观察,工程阵列正在使用,服务也没有被摧毁。因此它保留数据。 有三种方法可以解决此

  1. 当组件被破坏时,

    清晰的作品数组。

  2. 在组件的提供商数组中加入服务,因此服务实例与组件破坏了。

      @component({提供者:[Workservices]}))
     
  3. 每次称为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

  1. Clear works array when component is destroyed.

  2. include service in provider array of component so service instance destroys with component.

    @Component({providers:[WorkServices]})
    
  3. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文