我的项目运行正常。我刚刚在控制台网络中发现,我的GET
请求之一发送了两次,甚至我只发送了一次。 See network console
如果我注释created
函数的整个代码,则所有GET
请求都将不再加载/存在于控制台网络中。 (请参见下面的代码)
我想知道是什么原因造成的,应该如何解决?
这是 Component.vue
<script>
export default {
created: async function() {
await this.$store.dispatch('file/all');
},
};
</script>
vuex模块 post.js 的操作:
const actions = {
all({commit}, data) {
return axios.get(`files`)
.then(response => {
commit('setData', response);
});
},
}
答案 0 :(得分:0)
经过数小时的搜索,我发现分配给 Component 的key
引起了问题。
修改key
后,GET
请求将再次发送。这就是它发送两次的原因。特别感谢@Anatoly给我的提示。
以下是使用代码:
<template>
<Component :key="componentKey" @edit="dataIsChanged"/>
</template>
<script>
export default {
components: { Component },
data: () => ({
componentKey: 0,
}),
methods: {
dataIsChanged: function() {
this.componentKey = Math.random();
}
}
};
</script>