Vue.js updated() 不断更新无限次?
Vue.js updated() keeps on updating infinite times?
我有一个简单的 vue 组件,我在其中使用 beforeMounnt() 方法从数据库中获取一个问题,但在进行一些更改后我想重新加载 currentQuestion 我所在州的数据及其工作正常
但是更新后的方法不断从状态中获取新状态,这是我们所知的性能缺陷,谁能告诉我为什么会这样?
安装方法之前
async beforeMount() {
try {
const res = await axios.get(`/question/${this.questionId}`);
this.currentQuestion = { ...res.data };
} catch (err) {
this.error = err;
}
},
数据在Vue.js
data() {
return {
error: "",
success: "",
currentQuestion: {},
newChoiceDescription: "",
totalChoices: 0
};
},
更新方法
updated() {
console.log("Changing state");
const res = axios
.get(`/question/${this.questionId}`)
.then(res => {
// this.currentQuestion = res.data;
})
.catch(err => {
this.error = err;
});
},
原因是因为您的 updated()
导致更新发生...这触发了 update
官方文档警告不要这样做,在这里查看:https://v3.vuejs.org/api/options-lifecycle-hooks.html#updated
尝试在 questionId
上使用 watch
而不是
我有一个简单的 vue 组件,我在其中使用 beforeMounnt() 方法从数据库中获取一个问题,但在进行一些更改后我想重新加载 currentQuestion 我所在州的数据及其工作正常 但是更新后的方法不断从状态中获取新状态,这是我们所知的性能缺陷,谁能告诉我为什么会这样?
安装方法之前
async beforeMount() {
try {
const res = await axios.get(`/question/${this.questionId}`);
this.currentQuestion = { ...res.data };
} catch (err) {
this.error = err;
}
},
数据在Vue.js
data() {
return {
error: "",
success: "",
currentQuestion: {},
newChoiceDescription: "",
totalChoices: 0
};
},
更新方法
updated() {
console.log("Changing state");
const res = axios
.get(`/question/${this.questionId}`)
.then(res => {
// this.currentQuestion = res.data;
})
.catch(err => {
this.error = err;
});
},
原因是因为您的 updated()
导致更新发生...这触发了 update
官方文档警告不要这样做,在这里查看:https://v3.vuejs.org/api/options-lifecycle-hooks.html#updated
尝试在 questionId
上使用 watch
而不是