一、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);
}

 

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐