angularjs - AngularJS + Ionic 5 标签导航
问题描述
对于我的原生 APP 项目,我有两个选项卡,一个是主页选项卡,另一个名为“我的帐户”的选项卡由登录页面和帐户页面共享。登录成功后,它可以导航到帐户页面,但是当我单击“我的帐户”选项卡时,它将导航回登录页面。登录后需要手动刷新页面,就不会出现这个问题了。
有没有手动刷新页面的解决方案?谢谢你。
下面是login.page.ts的代码:
verifyLogin(pgcode:string, password:string)
{
//call API to verify the login details
this.http.get(url, headers)
.subscribe((apiReturn : any) =>
{
if(apiReturn.error){
//if login failed
this.sendNotification(apiReturn.error);
this.router.navigateByUrl('/tabs/login', { replaceUrl: true }); //redirect to login page
}else{
//if login success
this.sendNotification(apiReturn.message);
this.router.navigateByUrl('/tabs/myaccount', { replaceUrl: true });
}
},
(error : any) =>
{
//unable to call the API
this.sendNotification('Something went wrong!');
this.router.navigateByUrl('/tabs/login', { replaceUrl: true }); //redirect to login page
});
}
下面是tab.page.ts的代码:
ngOnInit(){
this.loadCookie();
}
async loadCookie(){
const cookie = await Storage.get({key: 'cookie'}); //get cookie's value from localStorage(same like session)
//const cookie = '';
if(cookie.value == null){
this.cookieValue = null;
this.myAccTab = '/tabs/login';
console.log('FALSE:',null);
}else{
this.cookieValue = cookie.value; //assign {{cookieValue}} in html then will echo the cookie value in homepage
this.myAccTab = '/tabs/myaccount';
console.log('TRUE:',cookie.value);
}
}
下面是tabs.page.html的代码:
<ion-tab-bar slot="bottom">
<ion-tab-button tab="home" [routerLink]="['/tabs/home']">
<ion-icon name="home"></ion-icon>
<ion-label>Home</ion-label>
</ion-tab-button>
<ion-tab-button [routerLink]="myAccTab">
<ion-icon name="accessibility-outline"></ion-icon>
<ion-label>My Account</ion-label>
</ion-tab-button>
</ion-tab-bar>
解决方案
登录成功后,您必须this.loadCookie();
在标签页上调用。您可以在登录后发布事件并在标签页中订阅它。在事件订阅中,您可以调用该this.loadCookie()
函数。
推荐阅读
- weblog - Web 服务器日志数据中的最后一个字段是关于什么的?
- java - 如何“交换”数组中的两个值?
- visual-studio-code - ProcessException:进程“C:\Users\...\android\gradlew.bat”异常退出:
- java - Maven 在 Jenkins 上失败(目标存储库不能为空)但不是本地的
- wordpress - 如何在 Compute Engine 上为 1000 个活跃用户选择机器类型
- java - 如何让休眠不查询孩子?
- python - 如何在一个 for 语句中从头到尾访问项目
- amazon-web-services - Ansible 创建 aws 安全组并添加到实例
- javascript - 为什么 v-bind 参数不能引用对象中的变量?
- algorithm - 差异算法:根据实际成绩单获取口语音节的时间戳