Ionic 2种不同的菜单离子

0wi1tuuw  于 2023-01-15  发布在  Ionic
关注(0)|答案(1)|浏览(222)

我有2个不同的菜单为2个不同的用户,但当我注销和改变帐户,它保持旧的菜单加载,并没有改变它的新的。
这是我从用户那里获取数据的登录页面:

export class authPage {

    resposeData: any;
    cin: number;
    emeil: String;
    result = <any[]>[];
    data: any;
    activeMenu: string;

    constructor(public navCtrl: NavController, public authService: AuthService, private toastCtrl: ToastController, public menu: MenuController, private alertCtrl: AlertController) {

        //this.menu.enable(false,'appMenuItems');
        this.menu.enable(false);
    }

    ionViewDidLoad() {

        if (localStorage.getItem('id_en') !== null) {
            this.navCtrl.setRoot(NouveauxPubPage);
            this.menu.enable(true);

        }else if(localStorage.getItem('cin') !== null) {
            this.navCtrl.setRoot(HomePage);
            this.menu.enable(true);
        }

        console.log('ionViewDidLoad Login');
    }
    login() {
        console.log(this.cin, this.emeil);
        if (this.cin && this.emeil) {
            this.authService.auth(this.cin, this.emeil)
                .subscribe((data) => {
                       this.result = data;
                    if (this.result[1] == "enseignant") {
                        this.navCtrl.setRoot(NouveauxPubPage);
                        localStorage.setItem('id_en', data[0][0].id_en);
                        localStorage.setItem('type', 'enseignant');
                        this.menu.enable(true);
                    }
                    else if (this.result[1] == "etudiant") {
                        this.navCtrl.setRoot(HomePage);
                        localStorage.setItem('cin', data[0][0].cin);
                        localStorage.setItem('type', 'etudiant');
                        this.menu.enable(true);

                    }
                    else {
                        let alert = this.alertCtrl.create({
                            title: 'Erreur connexion',
                            subTitle: "l'identifiant ou le mot de pass ne sont pas correcte",
                            buttons: ['Ok']
                        });
                        alert.present();
                        console.log('error');
                    }
                }, (err) => {
                    let alert = this.alertCtrl.create({
                        title: 'Erreur Serveur',
                        subTitle: "Verifier votre internet",
                        buttons: ['Ok']
                    });
                    alert.present();
                    console.log('error');
                });
        }
    }

    presentToast(msg) {
        let toast = this.toastCtrl.create({
            message: msg,
            duration: 2000
        });
        toast.present();
    }

}

这是应用程序组件,包含菜单和有关应用程序的所有内容

export class MyApp {
    @ViewChild(Nav) nav: Nav;

    rootPage: any = authPage;
    appMenuItems: Array<MenuItem>;

    helpMenuItems: Array<MenuItem>;

    type: string;
    cin: any;
    id_en: any;

    pages: Array<{ title: string, component: any, icon: string }>;

    constructor(public platform: Platform, public statusBar: StatusBar, public splashScreen: SplashScreen, private app: App) {
        this.initializeApp();
    }

    initializeApp() {
        this.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.
            if (localStorage.getItem('cin') !== null) {
                this.cin = localStorage.getItem('cin')
                console.log('cin:' + this.cin)
                this.type = localStorage.getItem('type');
                console.log(this.type)
                this.statusBar.styleDefault();
                this.splashScreen.hide();
            }
            else if (localStorage.getItem('id_en') !== null) {
                this.id_en = localStorage.getItem('id_en');
                console.log('id_en:' + this.cin)
                this.type = localStorage.getItem('type');
                this.statusBar.styleDefault();
                this.splashScreen.hide();
            }
            else {
                this.statusBar.styleDefault();
                this.splashScreen.hide();
            }
            if (localStorage.getItem('id_en') !== null) {
                this.appMenuItems = [
                    {title: 'Nouveaux Annonces', component: NouveauxPubPage, icon: 'md-create'},
                    {title: 'Mes Annonces', component: HomeePage, icon: 'md-list-box'},
                    {title: 'Mes Etudiant', component: EtudiantPage, icon: 'ios-contacts'},
                    {title: 'Historique', component: HistoriquePage, icon: 'ios-undo'},
                    {title: 'A propos', component: exemplePage, icon: 'information-circle'},
                ];
            } else {
                // used for an example of ngFor and navigation
                this.appMenuItems = [
                    {title: 'Accueil', component: HomePage, icon: "md-home"},
                    {title: 'Stages', component: stagesPage, icon: "md-attach"},
                    {title: 'Nouveaux article', component: ETarticlePage, icon: "md-create"},
                    {title: 'Mes Articles', component: mesarticlePage, icon: "md-list-box"},
                    {title: 'Annonce', component: ETpubPage, icon: "md-clipboard"},
                    {title: 'Historique', component: EThistoriquePage, icon: "ios-undo"},
                    {title: 'Stage en cours', component: stage_en_courPage, icon: "md-alarm"},
                    {title: 'A propos', component: exemplePage, icon: 'information-circle'},
                ];
            }
        });
    }

    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);

    }

    deconnexionetudiant() {
        localStorage.removeItem('cin');
        localStorage.removeItem('id_en');
        localStorage.removeItem('type');
        this.nav.setRoot(authPage);
        window.location.reload();
    }

如果有人能帮忙,我会很感激的

lnlaulya

lnlaulya1#

你需要刷新菜单,每当用户登录时,尝试调用app.component.ts内的事件。

events.subscribe("child:login", () => {
    //REFRESH THE MNU
})

所以从auth.ts,类似这样的东西。

login() {
  this.events.publish("child:login");
}

相关问题