Laravel Vue.js updated()不断更新无限次?
我有一个简单的vue组件,其中我使用beforeMounnt()方法从数据库中提取问题,但在做了一些更改后,我想在我的状态下重新加载currentQuestion数据,它工作正常 但是更新后的方法不断从状态中获取新状态,这是一个性能陷阱,我们知道,有人能告诉我为什么会发生这种情况吗 安装方法之前Laravel Vue.js updated()不断更新无限次?,laravel,vue.js,vuejs2,vue-component,Laravel,Vue.js,Vuejs2,Vue Component,我有一个简单的vue组件,其中我使用beforeMounnt()方法从数据库中提取问题,但在做了一些更改后,我想在我的状态下重新加载currentQuestion数据,它工作正常 但是更新后的方法不断从状态中获取新状态,这是一个性能陷阱,我们知道,有人能告诉我为什么会发生这种情况吗 安装方法之前 async beforeMount() { try { const res = await axios.get(`/question/${this.quest
async beforeMount() {
try {
const res = await axios.get(`/question/${this.questionId}`);
this.currentQuestion = { ...res.data };
} catch (err) {
this.error = err;
}
},
updated() {
console.log("Changing state");
const res = axios
.get(`/question/${this.questionId}`)
.then(res => {
// this.currentQuestion = res.data;
})
.catch(err => {
this.error = err;
});
},
Vue.js中的数据
data() {
return {
error: "",
success: "",
currentQuestion: {},
newChoiceDescription: "",
totalChoices: 0
};
},
更新方法
async beforeMount() {
try {
const res = await axios.get(`/question/${this.questionId}`);
this.currentQuestion = { ...res.data };
} catch (err) {
this.error = err;
}
},
updated() {
console.log("Changing state");
const res = axios
.get(`/question/${this.questionId}`)
.then(res => {
// this.currentQuestion = res.data;
})
.catch(err => {
this.error = err;
});
},
原因是您的
updated()
导致发生更新。。。这将触发更新
官方文件警告不要这样做,请在此处查看:
尝试在问题ID
上使用观察