Vue.js 以编程方式选中复选框不会';对对象使用嵌套数组时,不呈现更改

Vue.js 以编程方式选中复选框不会';对对象使用嵌套数组时,不呈现更改,vue.js,vuejs2,Vue.js,Vuejs2,我有一个带有类别的数组。每个类别都有子类别 选中/取消选中父项时,其子项也必须选中/取消选中。如果选中了所有子项,则必须同时选中父项 Vue会按预期更新字段,但不会重新渲染。我不明白为什么 这是我的密码: <template> <div class="card"> <div class="card-body"> <ul class="list-tree">

我有一个带有类别的数组。每个类别都有子类别

选中/取消选中父项时,其子项也必须选中/取消选中。如果选中了所有子项,则必须同时选中父项

Vue会按预期更新字段,但不会重新渲染。我不明白为什么

这是我的密码:

<template>
    <div class="card">
            <div class="card-body">
                    <ul class="list-tree">
                        <li v-for="category in categories" :key="category.id" v-show="category.show">
                            <div class="custom-control custom-checkbox">
                                <input :id="'category-' + category.id"
                                             v-model="category.checked"
                                             @change="checkParent(category)"
                                             type="checkbox"
                                             class="custom-control-input" />
                                <label class="custom-control-label"
                                             :for="'category-' + category.id">
                                    {{ category.name }}
                                </label>
                            </div>
                            <ul>
                                <li v-for="child in category.children" :key="child.id" v-show="child.show">
                                    <div class="custom-control custom-checkbox">
                                        <input :id="'category-' + child.id"
                                                     v-model="child.checked"
                                                     @change="checkChild(child, category)"
                                                     type="checkbox"
                                                     class="custom-control-input" />
                                        <label class="custom-control-label" :for="'category-' + child.id">
                                            {{ child.name }}
                                            <small class="counter">({{ child.products_count }})</small>
                                        </label>
                                    </div>
                                </li>
                            </ul>
                        </li>
                    </ul>
                </div>
    </div>
</template>


您需要扩展您的范围,因为您仅在
checkParent()
方法中对其进行更改,所以您所更改的变量不会对组件变量产生影响

在类别迭代中使用索引而不是值来查找正确的类别,然后在整个组件的范围中应用更改:

<li v-for="(category, categoryIndex) in categories" :key="category.id" v-show="category.show">
     <div class="custom-control custom-checkbox">
                                    <input :id="'category-' + category.id"
                                                 v-model="category.checked"
                                                 @change="checkParent(categoryIndex)"
                                                 type="checkbox"
                                                 class="custom-control-input" />
                                    <label class="custom-control-label"
                                                 :for="'category-' + category.id">

                                    {{ category.name }}     
                     </label>
              </div> <!-- the rest of the code ... -->

我修好了。问题根本与复选框无关。问题与我创建类别数组的方式有关

初始化组件时,我会从vuex复制阵列,并添加新属性(如
已选中
),以便在选中父级时检查子级。我没有遵守规定,这就是为什么孩子们没有反应,在检查家长时也没有接受检查


非常感谢你努力帮助我

谢谢你的回复。不幸的是,这不起作用:(
<li v-for="(category, categoryIndex) in categories" :key="category.id" v-show="category.show">
     <div class="custom-control custom-checkbox">
                                    <input :id="'category-' + category.id"
                                                 v-model="category.checked"
                                                 @change="checkParent(categoryIndex)"
                                                 type="checkbox"
                                                 class="custom-control-input" />
                                    <label class="custom-control-label"
                                                 :for="'category-' + category.id">

                                    {{ category.name }}     
                     </label>
              </div> <!-- the rest of the code ... -->
methods: {

    checkParent (categoryIndex) {
       let categoryChecked = this.categories[categoryIndex];
        this.categories[categoryIndex].children.forEach((child, childIndex) => {
            this.categories[categoryIndex].children[childIndex].checked = categoryChecked;
        })
    },