美文网首页前端Vue专辑
vue 中用localstorage 和vuex 存值的方法

vue 中用localstorage 和vuex 存值的方法

作者: 小水嘀哩哒 | 来源:发表于2020-05-29 18:58 被阅读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'))

    相关文章

      网友评论

        本文标题:vue 中用localstorage 和vuex 存值的方法

        本文链接:https://www.haomeiwen.com/subject/bsjtzhtx.html