Autosave the form/model content on route change in angular 4

▼魔方 西西 提交于 2019-12-06 07:14:01

问题


In angular 4 application, I am moving from one module to other I want to save my content automatically so I need to identify the change in route.

class MyClass {
  constructor(private router: Router) {
    router.events.subscribe((val) => {
        // see also 
        console.log(val instanceof NavigationEnd) 
    });
  }
}

I have used this code but this call is incrementing for every visit. Its similar to Gmail compose how it saves data to draft when I moved from composing screen to other.


回答1:


You are leaving the subscription open when leaving, so it will increment. You need to unsubscribe:

import { Subscription } from 'rxjs/Subscription';

// ...

sub = new Subscription();

// ...

this.sub = router.events.subscribe((val) => {
    // see also 
    console.log(val instanceof NavigationEnd) 

});

// ...

ngOnDestroy() {
  this.sub.unsubscribe();
}


来源:https://stackoverflow.com/questions/48577164/autosave-the-form-model-content-on-route-change-in-angular-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!