【发布时间】:2021-01-21 23:34:33
【问题描述】:
在下面的 VueJS 2 组件中,我可以将 imgdata 属性添加到 area.questions 数组中的每个问题。它有效 - 我可以从 console.log 看到 imgdata 有价值的问题。但是尽管使用了 $set 它仍然不是响应式的,并且 imgdata 不在视图中!我怎样才能使这个反应?
var componentOptions = {
props: ['area'],
data: function() {
return {
qIndex: 0,
};
},
mounted: function() {
var that = this;
that.init();
},
methods: {
init: function() {
var that = this;
if (that.area.questions.length > 0) {
that.area.questions.forEach(function(q) {
Util.HTTP('GET', '/api/v1/photos/' + q.id + '/qimage').then(function(response) {
var thisIndex = (that.area.questions.findIndex(entry => entry.id === q.id));
var thisQuestion = (that.area.questions.find(entry => entry.id === q.id));
thisQuestion.imgdata = response.data;
that.$set(that.area.questions, thisIndex, thisQuestion);
})
});
}
console.log("area.questions", that.area.questions);
},
【问题讨论】:
-
道具不能是反应式的。如果您仍在寻求帮助,请告诉我
-
@PunitPatel 道具绝对是被动的
-
@Phil 是的,Phil 你是对的,它不能从组件中变异。要更改它,我们应该使用 $emit。