vue登陆后页面数据渲染不上,刷新才好,怎么解决?

我在登陆页面登录成功后我把用户信息放到了sessionstorage里,然后跳转到用户页面,里面的组件获取sessionstorage里面的数据然后渲染,但是发现第一次进来时数据并没有渲染上,刷新一次就好了,然后我在登陆后的页面的组件比如nav里面的created和mounted里面打印sessionstorage,发现即使是第一次登陆也能打印出来,但是数据没渲染上,这是怎么回事啊,求解?

//这是登录页面的
login() {
            console.log('登录');

            this.$axios
                .post('/auth/login', {
                    // account: this.tel,
                    // password: this.password,
                    account: '11111111111',
                    password: '111111',
                })
                .then(res => {
                    const o = res.data;
                    console.log('ok');
                    this.$notify({
                        title: '登陆成功',
                        message: '欢迎回来',
                        type: 'success',
                    });
                    console.log(o.data);
                    sessionStorage.userInfo = JSON.stringify(o.data);
                    if (o.data.type == 1) {
                        console.log('teacher');         
                        this.$router.push({ path: 'teacher' });

                    }else{
                        this.$router.push({ path: 'agent' });

                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
//这是登陆后的页面的导航头部组件
export default {
    name: 'teaTopNav',
    data() {
        return {
            userInfo: {},
        };
    },

    
    created() {
        console.log('这是created');
        console.log(JSON.parse(sessionStorage.userInfo));
    },
    mounted() {
        console.log('这是mounted');
        console.log(JSON.parse(sessionStorage.userInfo));
        this.userInfo = this.GLOBAL.USERINFO;
        console.log(this.userInfo);
    },
    computed: {},

    methods: {
        quit() {
            this.$router.push({ path: '/' });
        },
    },
};
阅读 4.8k
3 个回答

试试

this.$set(this, 'userInfo', JSON.parse(sessionStorage.userInfo))

teaTopNav 的模板贴出来看看

this.userInfo = this.GLOBAL.USERINFO 改成 this.userInfo = JSON.parse(sessionStorage.userInfo)不行么

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题