【问题标题】:how to redirect to a page from a method by selecting an option in sidemenu如何通过选择侧面菜单中的选项从方法重定向到页面
【发布时间】:2017-03-31 16:31:34
【问题描述】:

ionic start mySideMenu sidemenu --v2

使用这个我创建了一个侧边菜单,我正在做一些登录-注销的事情,所以我将用户详细信息存储在名为“userDetails”的本地存储变量中。当我从侧面菜单单击注销选项时,它被重定向到登录页面。

import { homePage } from '../pages/home/home';

import { DetailsPage } from '../pages/Details/Details';

import { AddclientPage } from '../pages/Addclient/Addclient';
import { Storage } from '@ionic/storage';
import { LoginPage } from '../pages/login/login';


@Component({
  templateUrl: 'app.html'
})
export class MyApp {
 @ViewChild(Nav) nav: Nav;
  rootPage: any =  homePage;
  
  pages: Array<{title: string, component: any,icon:string}>;
  
  constructor(platform: Platform,public storage:Storage,public mqttservice:Mqttservice) {
  
    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();
    });
     this.pages = [
      { title: 'Home ', component: homePage ,icon:"home"},
           
      { title:'Addclient',component:AddclientPage,icon:'add'},
     
      {title: 'Users' ,component:DetailsPage,icon:"people"},
      { title:'addclient',component:AddclientPage,icon:"person-add"},
      {title:'Logout',component:LoginPage,icon:"log-out"}
      
    ];
  }
 
  openPage(page) {
    // Reset the content nav to have just this page
    // we wouldn't want the back button to show in this scenario
    this.nav.setRoot(page.component);
  }

}

但我想要的是当用户单击注销时,删除本地存储变量,然后重定向到登录页面 所以我写了一个删除“userDetails”并重定向到“loginpage”的方法,但出现“无法读取未定义的属性'setRoot'”之类的错误

import { homePage } from '../pages/home/home';

import { DetailsPage } from '../pages/Details/Details';

import { AddclientPage } from '../pages/Addclient/Addclient';
import { Storage } from '@ionic/storage';
import { LoginPage } from '../pages/login/login';


@Component({
  templateUrl: 'app.html'
})
export class MyApp {
 @ViewChild(Nav) nav: Nav;
  rootPage: any =  homePage;
  
  pages: Array<{title: string, component: any,icon:string}>;
  
  constructor(platform: Platform,public storage:Storage,public mqttservice:Mqttservice) {
  
    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();
    });
     this.pages = [
      { title: 'Home ', component: homePage ,icon:"home"},
           
      { title:'Addclient',component:AddclientPage,icon:'add'},
     
      {title: 'Users' ,component:DetailsPage,icon:"people"},
      { title:'addclient',component:AddclientPage,icon:"person-add"},
      {title:'Logout',component:this.logout(),icon:"log-out"}
      
    ];
  }
  logout(){
  this.storage.remove('userDetails);
  this.nav.setRoot(LoginPage);
  }
 
  openPage(page) {
    // Reset the content nav to have just this page
    // we wouldn't want the back button to show in this scenario
    this.nav.setRoot(page.component);
  }

}

【问题讨论】:

    标签: angular typescript ionic-framework ionic2


    【解决方案1】:

    您在尝试创建pages 属性时调用了注销方法。请修改如下:

       this.pages = [
          { title: 'Home ', component: homePage, icon:"home" },   
          { title:'Addclient', component:AddclientPage, icon:'add' },
          { title: 'Users', component:DetailsPage, icon:"people" },
          { title:'addclient', component:AddclientPage, icon:"person-add" },
          { title:'Logout', component:null, icon:"log-out" }
        ];
    

    注意 logout 选项有 null 作为组件,而不是 logout 方法。现在在你的openPage 方法中:

      openPage(page) {
    
        if(!page.component) {
          // Only the logout has the component as null
          this.logout();
        } else {
          // Reset the content nav to have just this page
          // we wouldn't want the back button to show in this scenario
          this.nav.setRoot(page.component);
        }
      }
    

    【讨论】:

      猜你喜欢
      • 2016-06-13
      • 2014-08-03
      • 2018-09-28
      • 1970-01-01
      • 2018-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多