ionic3+angular4动态设置入口页rootpage,根据不同的状态值来确定不同的入口页 (实用、赞)

175 篇文章 1 订阅
139 篇文章 0 订阅

一、ionic3+angular4动态设置入口页rootpage,根据不同的状态值来确定不同的入口页

在ionic+angular4的项目中有一个需求,就是当项目启动后先自动调一个接口,这个接口返回四个不同的状态值,然后项目根据这个四个不同的状态值来设置不同的入口页;
例如状态值为1是入口页为登录页,状态值为2时,入口页为首页,状态值为3时入口页为验证身份页等依次类推,动态的设置入口页rootpage。
实现如下:
1.将rootpage的初始值设置为空
2.然后在app.component.ts里面设置项目启动的时候调用接口,获取返回的状态值
3.根据不同的状态值来设置不同的rootpage页面
 

export class MyApp {
  rootPage:any = "";
 
public AccountId=localStorage.getItem("AccountId");

constructor(
    platform: Platform, 
    statusBar: StatusBar, 
    splashScreen: SplashScreen,
    public mineService: MineService
) {
    platform.ready().then(() => {//当平台准备好的时候运行
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      statusBar.styleDefault();
      splashScreen.hide();

      mineService.ionienter(this.Id).then((data:string)=>{//调用接口
        let reslObj=JSON.parse(data);
        console.log(reslObj);
        console.log(0);
        console.log(reslObj.Status);
        if(reslObj.IsOK==1){
          if(reslObj.Status=="1"){//根据返回的状态值来跳转页面
            console.log(1);
            this.rootPage="HomePage";
          } else if(reslObj.Status=="2"){
            console.log(2);
            this.rootPage="MinePage1";
          } else if(reslObj.Status=="3"){
            console.log(3);
            this.rootPage="LoginPage";
          }
        }
      });
    });
  }
}

二、ionic2修改默认首页,根页面
ionic2中默认的根页面(即App首页)是TabsPage,如果要修改需要在app.component.ts文件中修改:

export class MyApp {
  rootPage: any = TabsPage;
  constructor(platform: Platform) {
    if (!isLogin()) {
      this.rootPage = LoginPage;
    }
}

 上面就是把默认首页修改为登录页LoginPage

如果登录成功后,可再动态修改回来根页面,在login.ts中再重新设置rootPage为TabsPage,增加以下内容:

import {App } from 'ionic-angular';
import { TabsPage } from '../tabs/tabs';
 
constructor(private app: App){}
 
loginOnSuccess(){
    this.app.getRootNav().setRoot(TabsPage);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值