在角度4中自动保存路线更改时的表单/模型内容 [英] Autosave the form/model content on route change in angular 4
本文介绍了在角度4中自动保存路线更改时的表单/模型内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在angular 4应用程序中,我正在从一个模块迁移到另一个模块,我想自动保存内容,因此我需要确定路线的变化.
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)
});
}
}
我已使用此代码,但是每次访问此调用都在增加. 它与Gmail相似,构成了当我从撰写屏幕移到其他屏幕时如何保存数据以草稿的方式.
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.
推荐答案
您要在离开时将订阅保持打开状态,因此订阅会增加.您需要退订:
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();
}
这篇关于在角度4中自动保存路线更改时的表单/模型内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文