vuex 和 axios

1. 改

  想要变state的值,只能通过mutations,但是mutaions是同步执行,无法进行异步操作,比如ajax获取到数据再幅值,mutations无法实现,

  只能通过actions进行异步操作取值,然后commit(提交)mutations,mutaions自动执行,改写state的值。

const store = new Vuex.Store({
    state: {
        tasklist: []
    },
    getters: {
        getTaskList: state => state.tasklist
    },
    mutations: {
        addTaskList (state, res) {
            state.tasklist = res
        }
    },
    actions: {
        getTaskList (context) {
            var that = this
            axios.get(APIURL + '/task', {
                params: {
                    'Auth-key': 'welcome'
                }
            }).then(function(response) {
                context.commit('addTaskList', response.data)
            })
        }
    }

})

2. 查

  组建中无法直接用this.$store.state 查值。

  需要在store内,添加getters对象,让其向外暴露一个方法,组件中可以通过this.$store.getter获取该方法,获取state的值。

 

3. 前端组件怎么改store中的state?

  this.$store.dispatch('actions对象的内置方法')

posted @ 2017-05-18 10:40  涵叔  阅读(204)  评论(0)    收藏  举报