[英]Infinite Recursion When Trying To Splice A Vuex Array
I have a codesandbox setup up here which should be printing off three dates. 我这里有一个codeandbox设置,应该打印三个日期。
The most relevent pieces of code are: 最相关的代码是:
import Vue from "vue";
import App from "./App";
import Vuex from "vuex";
Vue.use(Vuex);
const { DateTime } = require("luxon");
Vue.config.productionTip = false;
var store = new Vuex.Store({
debug: true,
state: {
dateTimes: [
{ startTime: DateTime.local(), meta: "test" },
{ startTime: DateTime.local().plus({ days: 2 }), meta: "test" }
]
},
mutations: {
addItem(state) {
var test = {
startTime: DateTime.local().plus({ days: 1 }),
meta: "test"
};
for (var i = 0; i < state.dateTimes.length; i++) {
if (state.dateTimes[i].startTime > test.startTime) {
state.dateTimes.splice(i, 0, state.dateTimes);
}
}
state.dateTimes.push(test);
}
}
});
new Vue({
el: "#app",
store: store,
components: { App },
template: "<App/>",
created: function() {
this.$store.commit("addItem");
}
});
The specific error message I get is Error in render: "InternalError: too much recursion"
我收到的特定错误消息是Error in render: "InternalError: too much recursion"
What is the correct way to splice in item in Vuex array? 在Vuex数组中拼接项目的正确方法是什么?
This is the problem state.dateTimes.splice(i, 0, state.dateTimes);
这就是问题state.dateTimes.splice(i, 0, state.dateTimes);
You're always adding the same dates back in as state.dateTimes
will have the values in as the slice operation hasn't affected the array yet. 您总是要添加与state.dateTimes
相同的日期state.dateTimes
将具有值,因为slice操作尚未影响数组。
Simple solution is to remove it to be state.dateTimes.splice(i, 0, test);
一个简单的解决方案是将其删除为state.dateTimes.splice(i, 0, test);
This may not be the solution you want but it will fix the max call stack error 这可能不是您想要的解决方案,但是它将解决最大调用堆栈错误
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.