我在东北烤地瓜

I am kaodigua I am chirourou I am qiaodaima

导航

在Vue退出组件前,将不为空的参数保存到本地

    // 退出页面前将data中不为空的值到存到sessionstorage中,在进入页面时判断本地是否有当前页面name,有则赋值无则正常刷新
    //   存store也可
    beforeRouteLeave(to, form, next) {
        console.log(this)
        let a = [],
            b = {}
        let reg = /^(?![\$,_,__,rowStyle]).*/
        for (var key in this) {
            if (
                typeof this[key] !== 'function' &&
                this[key] !== '' &&
                reg.test(key)
            ) {
                if (Array.isArray(this[key]) && this[key].length == 0) {
            
                } else {
                    b[key] = this[key]
                }
            } else {
      
            }
        }
        sessionStorage.setItem(this.$options.name, JSON.stringify(b))

        next()
    },



    mounted: function() {
        if (sessionStorage.getItem(this.$options.name)) {
            console.log(JSON.parse(sessionStorage.getItem(this.$options.name)))
            let a = JSON.parse(sessionStorage.getItem(this.$options.name))
            for (var key in a) {
                this[key] = a[key]
            }
            sessionStorage.removeItem(this.$options.name);

        } else {
            this.auditType = 1
            this.enterTableQuery()
        }

    },

posted on 2021-11-15 13:50  我在东北烤地瓜  阅读(157)  评论(0编辑  收藏  举报