为什么我的财产不会反映铁本地存储价值?

时间:2017-05-27 19:22:31

标签: polymer local-storage polymer-1.0 polymer-2.x

我有一个登录系统,我需要在localstorage上记录登录用户,一旦用户登录,我想在他访问登录页面时将他重定向到他自己的页面。

我的模板:

<iron-localstorage name="user-storage" value="{{storedUser}}"></iron-localstorage>

我的对象:

        static get properties() {
            return {
                storedUser: {
                    type: Object,
                    notify: true
                },
                ...
            }
        }

我想这样做:

        redirect() {
            console.log(this.storedUser);
            if(this.storedUser) {
                // Redirect user or admin to their own homescreen
                if(this.storedUser.role == 'user')
                    this.set('route.path', '/homescreen-usuario');
                else if(this.storedUser.role == 'admin')
                    this.set('route.path', '/homescreen-admin');
                else
                    this.set('route.path', '/my-view404');
            }
        }

但第一行总是记录“未定义”。

1 个答案:

答案 0 :(得分:0)

您应该使用'on-iron-localstorage-load-empty'和'on-iron-localstorage-load'事件来响应存储事件,或者您可以在'storedUser'属性上使用观察者。< / p>

相关问题