前端Vue专辑

vue 中用localstorage 和vuex 存值的方法

2020-05-29  本文已影响0人  小水嘀哩哒

 1.vuex

const  store = {

        state:{ id:0 },

        mutations:{   SET_ID(state,id){state.id=id}   },

        actions:{ setId(commit,data){ commit ('SET_ID',data)}}

}

在页面中如何获取值和更改值

获取:

computed: {

    ...mapState({

      id: state => state.store.id

    })

  },

更改:

methods:{

       ...mapActions([ 'SetId' ]),

        //点击事件

        clickEvents(){

                this.SetId(1)

           }

}

2.localStorage

更改值:

const data={name:'张三',age:32}

window.localStorage.setItem('detail', JSON.stringify(data));

取值

JSON.parse(localStorage.getItem('detail'))

上一篇下一篇

猜你喜欢

热点阅读