Vuex突变应该是函数,但“ mutations.default”为{}

时间:2019-02-14 13:12:18

标签: vue.js vuex vue-cli

我正在尝试获取数据并将其插入基本的html表中

<tr>
    <td v-for="item in dataTable" :key="item.id">{{item.threadName}}</td>
</tr>

但是我在提交已挂载的函数时会遇到此错误。

mutation.js

import Vue from 'vue'

export default {
    receiveAll (state, data) {
        data.forEach(item => {
            return item
        })
    }
}

这是我在 actions.js

中使用它的地方
import * as api from '../api'

export const getData = ({ commit }) => {
    api.getData(data => {
        commit('receiveAll', data)
    })
}

data.js

'use strict';

module.export = [
    {
        id: 'm_1',
        threadID: 't_1',
        threadName: 'Jing and Bill',
        authorName: 'Bill',
        text: 'Hey Jing, want to give a Flux talk at ForwardJS?',
        timestamp: Date.now() - 99999
    },
    {
        id: 'm_2',
        threadID: 't_1',
        threadName: 'Jing and Bill',
        authorName: 'Bill',
        text: 'Seems like a pretty cool conference.',
        timestamp: Date.now() - 89999
    }
]

api / index.js

const data = require('./data')
const LATENCY = 16

export function getData (cb) {
    setTimeout(() => {
        cb(data)
    }, LATENCY)
}

这是 vuex商店

import Vue from 'vue'
import Vuex from 'vuex'
import * as actions from './actions'
import * as mutations from './mutations'

Vue.use(Vuex)

const state = {
    getData: []
}

export default new Vuex.Store({
    state,
    actions,
    mutations
})

在我的组件内部,我只是启动了两个v模型和计算出的数据

<script>
    import { mapActions } from 'vuex'

    export default {
        name: 'SearchTable',
        data () {
            return {
                search_query: '',
                search_sort: ''
            }
        },
        computed: mapActions({
            dataTable: 'getData'
        })
    }
</script>

4 个答案:

答案 0 :(得分:0)

receiveAll (state, data){ ...更改为function receiveAll (state, data) { ...

尝试:

import Vue from 'vue'

const receiveAll = (state, data) => {
    return data.forEach(item => {
        return item
    })
}

export default {
  receiveAll 
}

答案 1 :(得分:0)

正确的突变是对象,它具有方法。 这是此示例的简短形式

 export default {
        receiveAll: function (state, data) {
            data.forEach(item => {
                state.getData.push(item)
            })
        }
   }

请参阅Method definitions


您必须插入数据以说明何时会发生突变

export default {
    receiveAll (state, data) {
        data.forEach(item => {
            state.getData.push(item)
        })
    }
}

答案 2 :(得分:0)

在您的 vuex存储文件中,更改从中导入的突变

import * as mutations from './mutations'

import mutations from './mutations'

由于您要导出一个对象而不是单独的变量,
您无法从文件中导入所有文件(也称为*)。

答案 3 :(得分:0)

检查是否正确关闭了Mutation Object的花括号,这是一个简单的错误,但给出的错误信息相同。

mutations: {
     getAppIds: (state, payload) => {

   }
  <== this curly braces was missing for me