2种不同的菜单离子

时间:2018-06-01 14:54:22

标签: angular authentication ionic-framework menu logout

嗨开发人员我为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();
    }
如果任何人可以提供帮助,我会非常感激

1 个答案:

答案 0 :(得分:0)

您需要刷新菜单。每当用户登录时,尝试在app.component.ts中调用一个事件。

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

所以从auth.ts,这样的事情。

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