【发布时间】:2018-10-01 02:07:58
【问题描述】:
我有几个使用 vue-tables-2 的组件,但其中一个在我更改路由之前不会更新表格。
组件
<template>
//..
<div class="table-responsive" >
<v-client-table ref="table" name="vCardTable"
:data="vCardTableData.data"
:columns="vCardTableData.headers"
:options="vCardTableData.options"/>
</div>
//..
</template>
<script>
import { mapState } from "vuex";
import { mapGetters } from "vuex";
export default {
name: "VCard",
computed: {
...mapState("commons", ["user"]),
...mapGetters({ vCardTableData: "vCard/vCardTableData" })
},
mounted() {
var self = this;
self.$nextTick(() => {
self.$store.dispatch("vCard/getVCards"); <-- GET TABLE DATA
});
}
};
</script>
商店
const state = {
vCardTableData: {
data: [],
headers: [
//..
],
options: {
filterable: false,
preserveState: false,
headings: {
//..
},
texts: {
//..
},
pagination: {
dropdown: true,
},
templates: {
//..
},
},
}
}
const getters = {
vCardTableData: state => state.vCardTableData
}
const actions = {
getVCards({commit, dispatch}) {
return api.request("get", "getvcards").then(response => {
setTimeout(() => {
commit("setVCardTableData", response.data.vcards);
}, 300);
}).catch(error => {
console.log(error);
});
}
}
const mutations = {
clearTableData: (state) => {
if (state.vCardTableData.data) {
state.vCardTableData.data = [];
}
},
setVCardTableData : (state, vCardTableData) => state.vCardTableData.data = vCardTableData
}
正如您在这张图片中看到的,该表有数据:
2018 年 2 月 5 日
现在我已经看到,如果我直接在组件中使用 Promise 修改状态,它会起作用:
this.$store.dispatch("vCard/getVCards", []).then((responseData)=>{
this.$store.state.vCard.vCardTableData.data = responseData;
});
有人知道为什么吗? 谢谢
【问题讨论】:
-
可能是因为它需要某种“深度”跟踪。使用 vuex 查找该关键字。
-
试试
useVuex=true和Vue.use(VueTables.ClientTable, [options = {}], [useVuex = true], [theme = 'bulma'], [template = 'default'])一样,我也测试了和你类似的代码,看起来工作正常.. -
“但是其中一个在我改变路线之前不会更新表格。”,你认为它应该改变以响应什么?点击一个按钮?呼吁采取行动?如果有,是哪一个?
-
正在调用一个动作 -> self.$nextTick(() => { self.$store.dispatch("vCard/getVCards"); });
标签: vue.js vuex vue-tables-2