Ionic 3 - TypeError:无法设置属性' userRole'未定义的

时间:2017-10-01 13:46:48

标签: angular typescript ionic3 ionic-events

尝试使用事件替换登录用户的Ionic应用程序中的菜单。

Login.ts

publishLoginDetails = function (_userId, _userRole) {
    this.events.publish('login:success', _userRole);
};

app.component.ts

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

constructor(public platform: Platform, public statusBar: StatusBar, public splashScreen: SplashScreen, public events: Events) {
    this.initializeApp();

    this.pages = [
      { title: 'Home', component: HomePage, icon: 'home' },
      { title: 'Login', component: LoginPage, icon: 'log-in' }
    ];

    events.subscribe('login:success', function (uRole) {
        if (uRole !== undefined && uRole !== "") {
            this.userRole = uRole;
            switch (uRole) {
                case 'admin': {
                    this.pages = [
                    { title: 'Home', component: HomePage, icon: 'home' },
                    { title: 'Link1', component: Link1Page, icon: 'list' },
                    { title: 'Link2', component: Link2Page, icon: 'person' },
                    { title: 'Logout', component: '', icon: 'log-out' }
                    ];
                    break;
                }
                case 'employee': {
                    this.pages = [
                    { title: 'Home', component: HomePage, icon: 'home' },
                    { title: 'Link1', component: Link1Page, icon: 'people' },  
                    { title: 'Logout', component: '', icon: 'log-out' }
                    ];
                    break;
                }
            }
        }
    }
}

获取

enter image description here

因此将userRole的声明修改为

userRole: string = "user";
pages: Array<{ title: string, component: any, icon: string }> = [];

由于它不起作用,我删除了node_modules并再次运行&#34; npm install&#34;。 我也跑了&#34; npm cache clean&#34;但同样的错误。

请指导我如何解决此问题。

感谢。

1 个答案:

答案 0 :(得分:0)

像@yurzui所说,使用箭头功能。

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

constructor(public platform: Platform, public statusBar: StatusBar, public splashScreen: SplashScreen, public events: Events) {
    this.initializeApp();

    this.pages = [
      { title: 'Home', component: HomePage, icon: 'home' },
      { title: 'Login', component: LoginPage, icon: 'log-in' }
    ];

    events.subscribe('login:success', (uRole) => {
        if (uRole !== undefined && uRole !== "") {
            this.userRole = uRole;
            switch (uRole) {
                case 'admin': {
                    this.pages = [
                    { title: 'Home', component: HomePage, icon: 'home' },
                    { title: 'Link1', component: Link1Page, icon: 'list' },
                    { title: 'Link2', component: Link2Page, icon: 'person' },
                    { title: 'Logout', component: '', icon: 'log-out' }
                    ];
                    break;
                }
                case 'employee': {
                    this.pages = [
                    { title: 'Home', component: HomePage, icon: 'home' },
                    { title: 'Link1', component: Link1Page, icon: 'people' },  
                    { title: 'Logout', component: '', icon: 'log-out' }
                    ];
                    break;
                }
            }
        }
    }
}