vuex最简单的使用方法

2019-08-05  本文已影响0人  _喵了个喵_

引入第三方插件,有个监听,当第二次进入之后监听到的要更改视图,但是无果,用vuex最简单的解决此问题

1.引入vuex

2.创建store文件,添加index.js

import Vue from 'vue'

import Vuex from 'vuex'

Vue.use(Vuex)

const state = {

  arr:[],

  mentionedJson:{}

}

const mutations = {

    modifyArray(state, obj){

        if(!!obj)

        {

            state.arr= obj

        }

      },

}

export default new Vuex.Store({

  state,

  mutations,

})

3.main.js  

import store from './store/index.js'

new Vue({

    el: '#app',

    router,

    store,

    components: { App },

    template: '<App/>'

})

4.使用

computed:{

    historyList(){

      return this.$store.state.arr;  

    },

  },

事件中监听修改

let array = [1,2,3]

_this.$store.commit('modifyArray',array );

上一篇 下一篇

猜你喜欢

热点阅读