【问题标题】:Vuejs v-model overwritting arrayVuejs v-model 覆盖数组
【发布时间】:2018-11-04 11:41:07
【问题描述】:

我正在使用 Vuejs 2 和 laravel。目前我正在使用权限模块的选择框,它是一个复杂的框结构。我遇到的问题是当我尝试在 v-model 中绑定我的嵌套数组项时,它充当字符串。每次我选中一个框时,它都会覆盖变量。

workspace.selectedOperations 是正在覆盖的模型。

这是它的html:

<b-tab v-for="workspace in addedWorkspaces" :key="workspace.id" :title="workspace.title" active>
                    <div class="roles-permissions">
                        <label>Permissions</label>
                        <div class="permissions-path">
                            <ul class="pl-0">
                                <li v-for="entity in workspace.entities">
                                    <b-form-checkbox>{{entity.title}}</b-form-checkbox>
                                    <ul>
                                        <li v-for="operation in entity.operations">{{workspace.selectedOperations}}
                                        <b-form-checkbox v-model="workspace.selectedOperations" :value="operation.id">{{operation.title}}</b-form-checkbox>
                                        </li>
                                    </ul>
                                </li>
                            </ul>
                        </div>
                    </div>
                </b-tab>

这是脚本:

<script>
import Multiselect from 'vue-multiselect'
export default {
props : ['showModalProp'],
    data () {
        return {
            title : '',
            value: [],
            entities : [],
            addedWorkspaces : [],
            selectedWorkspaces : '',
        }
    },    
methods: {
    getOperationsList(){
        this.$http.get('entity').then(response=>{
            response = response.body.response;
            this.entities = response.data;
        });
    },
    showModal() {
        this.$refs.myModalRef.show()
    },
    hideModal() {
        this.$refs.myModalRef.hide()
    },
    onHidden(){
        this.$emit('HideModalValue');
    },
    addWorkspaces(){
        this.addedWorkspaces = this.selectedWorkspaces;
        this.selectedWorkspaces = [];
        for (var i = this.addedWorkspaces.length - 1; i >= 0; i--) {
            this.addedWorkspaces[i].entities = 
            JSON.parse(JSON.stringify(this.entities));
            this.addedWorkspaces[i].selectedOperations = [];
        }
    }        
},
mounted(){
    this.getOperationsList();
},
  components: {
    Multiselect
  },    
watch:{
    showModalProp(value){

        if(value){
            this.showModal();
        }
        if(!value){
            this.hideModal();
        }

    }
},
computed :{
    options(){
        return this.$store.getters.getWorkspaces;
    }
}
}  
</script>

【问题讨论】:

    标签: javascript data-binding vue.js vuejs2 model-binding


    【解决方案1】:

    如果你希望 b-form-checkbox 的 v-model 是一个数组,你应该用 b-form-checkbox-group 包装你的 b-form-checkbox 并在上面设置 v-model

    见https://bootstrap-vue.js.org/docs/components/form-checkbox

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-04
      • 2018-05-15
      • 2016-04-21
      • 2021-03-10
      • 2020-06-08
      • 2020-11-24
      • 2018-06-08
      • 2019-12-27
      相关资源
      最近更新 更多