【发布时间】:2020-07-25 05:07:09
【问题描述】:
我有一个数据,它是 {"1": {}} 之类的对象内的一个对象,它在 <template>.. 中没有反应性以下是我的代码
<template>
<div>
<b-container
class="bg-white text-center scrollBehavior"
>
<b-row
class="align-items-center selection-row"
v-for="(category, categoryIndex) in defaultSelectionCategories"
:key="categoryIndex"
>
<b-col cols="1" class="pl-1 pr-1">
{{ category }}
</b-col>
<b-col class="bg-white pt-2">
<b-container class="pl-1 pr-1">
<b-row class="mb-2" v-for="row in 3" :key="row">
<b-col
class="pl-1 pr-1"
variant="outline-light"
v-for="(selection, selectionIndex) in _.cloneDeep(
defaultSelection
).splice(
(row - 1) * 5,
row === 3 ? (categoryIndex + 1 > 5 ? 4 : 6) : 5
)"
:key="selectionIndex"
>
<b-button
block
size="sm"
variant="outline-light"
:class="[
'selection',
requestSelection['1']['0'] ? 'active' : ''
]"
@click="calculateRequest(categoryIndex + 1, selection)"
>
{{ selection }}
</b-button>
</b-col>
</b-row>
</b-container>
</b-col>
</b-row>
</b-container>
</div>
</template>
<script>
var hmItems = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"];
var dxItems = ["a", "b"];
var dsItems = ["c", "d"];
var lhItems = ["e", "f"];
var selection = [...hmItems, ...dxItems, ...dsItems, ...lhItems];
var selectionCategories = [
"aa",
"bb",
"cc",
"dd",
"ee",
"ff",
"gg",
"hh",
"ii",
"jj"
];
var requestSelection = {
1: {},
2: {},
3: {},
4: {},
5: {},
6: {},
7: {},
8: {},
9: {},
10: {}
};
export default {
data: function() {
return {
defaultSelection: this._.cloneDeep(selection),
defaultSelectionCategories: this._.cloneDeep(
selectionCategories
),
defaultSelection: this._.cloneDeep(requestSelection),
requestSelection: this._.cloneDeep(requestSelection)
};
}
methods: {
calculateRequest(categoryIndex, selection) {
console.log("Start Calculate Request");
console.log("CategoryIndex: " + categoryIndex);
console.log("Selection: " + selection);
if (hmItems.indexOf(selection) !== -1) {
if (
!this.requestSelection[categoryIndex]["0"] ||
!this.requestSelection[categoryIndex]["0"].length
) {
this.requestSelection[categoryIndex]["0"] = [selection];
} else {
const indexOf = this.requestSelection[categoryIndex]["0"].indexOf(
selection
);
if (indexOf === -1) {
this.requestSelection[categoryIndex]["0"].push(selection);
} else {
this.requestSelection[categoryIndex]["0"].splice(indexOf, 1);
}
}
} else if (dxItems.indexOf(selection) !== -1) {
this.requestSelection[categoryIndex]["1"] = selection;
} else if (dsItems.indexOf(selection) !== -1) {
this.requestSelection[categoryIndex]["2"] = selection;
} else if (lhItems.indexOf(selection) !== -1) {
this.requestSelection[categoryIndex]["3"] = selection;
}
console.log(
"this.requestSelection: " + JSON.stringify(this.requestSelection)
);
},
}
};
</script>
当我单击该按钮时,我在<b-button> 上的班级应该有一个active 班级,但它没有更新。正如console.log("this.requestSelection: " + JSON.stringify(this.requestSelection)); 中显示的那样,数据肯定会更新,但仍然没有任何反应。有趣的是,如果我输入 {1: { "0": ["1"] }, ...} 而不是 {1: {}},.. 它将正常工作。
我在这里错过了什么?
【问题讨论】:
-
这能回答你的问题吗? Vue Watch not triggering
-
不是真的,我没用手表
标签: javascript vue.js