【问题标题】:how to keep form data from nativescript-localstorage when routing in another component and return back in first component在另一个组件中路由并在第一个组件中返回时如何保留来自 nativescript-localstorage 的表单数据
【发布时间】:2019-06-07 06:18:33
【问题描述】:
【问题讨论】:
标签:
typescript
local-storage
nativescript
【解决方案1】:
您所要做的就是使用FormGroup 方法,.getRawValue() 将表单数据作为对象获取,.patchValue(data) 从对象设置表单值。
您还必须使用routerExtensions.back() 导航回您来自的页面。使用router.navigate(...) 将继续在此处打开新的主页实例。
ngOnInit(): void {
// Needs relative path to work with Playground
let LS = require("../nativescript-localstorage");
// Make sure you don't apply invalid values from your previous cache, you will get invalid boolean exception if then
this.ConfigurationForm.patchValue(JSON.parse(LS.getItem(ConfigurationComponent.CURRENT_CONFIG) || "{}"));
}
register() {
// Needs relative path to work with Playground
let LS = require("../nativescript-localstorage");
LS.setItem(ConfigurationComponent.CURRENT_CONFIG, JSON.stringify(this.ConfigurationForm.getRawValue()));
this.routerExtensions.back();
}
Updated Playground