极光推送收到通知,使用setRoot打开页面,数据绑定被解绑
2019-02-28 本文已影响0人
IT飞牛
极光推送收到通知,点击通知后,使用setRoot(page)方法设置导航控制器A的root页面,重载页面数据。执行完后模板页面变量不更新。
附:定义变量i,并使用setinterval定时器i++,,然后在前台输出{{i}}。在打开通知后,这个i还是能够定时累加输出的,并且,每次刷新都会顺带把type也刷新一遍。
执行的操作:
点击极光推送通知后,定位到tabs中的tab0的root页面(aPage),并重载aPage本页面的数据
大致页面结构:
有以下几个页面, mainPage, aPage, bPage, cPage.
aPage、bPage、cPage分别是tab0、tab1、tab2的root页面.
aPage页面segment组件双向绑定了type变量,代码如下:
<ion-header>
<ion-toolbar color="dark" mode="md" class="exx-segment">
<ion-segment [(ngModel)]="type">
<ion-segment-button value="item1">item1</ion-segment-button>
<ion-segment-button value="item2">item2</ion-segment-button>
</ion-segment>
</ion-toolbar>
</ion-header>
<ion-content no-padding [hidden]="type!='item1'">
...
</ion-content>
<ion-content no-padding [hidden]="type!='item2'">
...
</ion-content>
监听jpush.openNotification,执行以下代码重载aPage页面数据:
let currentTab = this.app.getActiveNavs()[0];
let tab0 = currentTab.parent.getByIndex(0) as Tab;
currentTab.parent.select(0);
currentTab.popToRoot().then(() => {
tab0.setRoot("aPage", { deftype: "param1" });
})
解决方案只需要确保以下几点:
1、为了确保收到通知时,当前tab能返回到根节点,每个tab中,不可以setRoot除了根root页面以外的页面。否则popToRoot无效。
2、对使用了setRoot方法的tab0导航控制器,tab0下的所有页面的数据绑定机制都会出现问题,使用ngZone进行绑定重置。
参考资料:
Ionic3界面数据不更新或者不刷新的处理方法
NgZone的简单使用
Ionic3与原生Android数据交互UI不更新
期间尝试了所有navController中用于控制页面进退的方法,都没有解决,这里附带测试过程中会导致这种现象的方法:
首先要说下
tabsHideOnSubPages
,当执行push时,tabsHideOnSubPages
如果为:true
:则在当前tab容器同级添加新页面div代码false
:则在当前tab容器内部添加新页面div代码(只要是在内部添加或修改,都会导致绑定机制失效)以下尝试重载页面的方法都有问题:
//方案一:
// let currentTab = this.app.getActiveNavs()[0]; //获取当前活动导航器
// let tab0 = currentTab.parent.getByIndex(0) as Tab; //获取待刷新页面所处导航器
// currentTab.parent.select(0); //切换tabs到tab0
// currentTab.popToRoot().then(() => {
// //在tab0中设置shiwu页面为首页,并刷新
// tab0.setRoot("shiwu", { deftype: "daiban" });
// })
//方案二:子页面中tabs隐藏问题没法解决,是否有push可以控制tabsHideOnSubPages的方法
// let currentTab = this.app.getActiveNavs()[0]; //获取当前活动导航器
// let tab0 = currentTab.parent.getByIndex(0) as Tab; //获取待刷新页面所处导航器
// currentTab.parent.select(0); //切换tabs到tab0
// currentTab.popToRoot().then(() => {
// //在tab0中设置shiwu页面为首页,并刷新
// tab0.push("shiwu", { deftype: "daiban" }).then(() => {
// tab0.remove(0,tab0.getActive().index);
// })
// })
//方案三
// let currentTab = this.app.getActiveNavs()[0];
// let rootNav = this.app.getRootNav()
// currentTab.parent.select(0);
// rootNav.push("main")
// .then(() => {
// rootNav.remove(0, rootNav.getActive().index);
// });
//方案四
// let currentTab = this.app.getActiveNavs()[0]; //获取当前活动导航器
// let rootNav = this.app.getRootNav();
// currentTab.parent.select(0);
// rootNav.setPages([{ page: "main" }, { page: "main" }, { page: "empty" }]).then(() => {
// setTimeout(() => {
// //注意:点击手机返回键,返回的页面一切正常。
// // this.bb.back();//失败,使用ionic3-android-backbutton插件,触发返回时间,类似pop
// // rootNav.pop();//失败
// // rootNav.popToRoot();//失败
// // history.back(-1)//失败
// }, 2000);
// });
//方案五,不会刷新选项卡:href
// location.href = location.href.slice(0,location.href.indexOf("#/main/"))+"#/main/1/shiwu?rand="+(new Date()).getTime();
// 方案六:replace
// location.replace(location.href.slice(0, location.href.indexOf("#/main/")) + "#/main/1/shiwu");
//方案七:insert
// let currentTab = this.app.getActiveNavs()[0]; //获取当前活动导航器
// let tab0 = currentTab.parent.getByIndex(0) as Tab;
// currentTab.parent.select(0);
// currentTab.popToRoot().then(() => {
// tab0.insert(0, "shiwu", { deftype: "daiban" }).then(() => {
// tab0.pop();
// });
// })
//方案:借助location.reload();有1秒左右白屏