简体   繁体   English

带有prop数组的Vue JS CheckBoxGroup作为v-model

[英]Vue JS CheckBoxGroup with a prop array as v-model

I am stuck at making a CheckBoxGroup with a prop array as v-model.我坚持使用道具数组作为 v-model 制作 CheckBoxGroup。 I have read the vuejs guide: https://v2.vuejs.org/v2/guide/forms.html#Checkbox which has the v-model array in the data of the same component, but it is obviously pretty useless if I want to make this component reusable and insert the v-model via props and for example check some of the boxes from "outside".我已经阅读了 vuejs 指南: https ://v2.vuejs.org/v2/guide/forms.html#Checkbox 在同一组件的数据中包含 v-model 数组,但如果我想要它显然没用使该组件可重用并通过道具插入 v-model,例如从“外部”检查一些框。 So I tried following:所以我尝试了以下操作:

CheckBoxgroup.vue复选框组.vue

<template>
  <div>
    <label v-for="day in allDays" :key="day">
      <input v-model="checkedDays" type="checkbox" :value="day" />
      <span>{{ day }}</span>
    </label>
    <div>Checked days: {{ checkedDays }}</div>
 </div>
</template>
<script lang="ts">
import Vue from 'vue'
import { Component, Prop } from 'vue-property-decorator'

@Component
export default class CheckBoxGroup extends Vue {
  @Prop() checkedDays!: string[]

  @Prop() allDays!: string[]
}
</script>

Index.vue索引.vue

<template>
  <div>
    <checkbox-group :checked-days="checkedDays" :all-days="allDays" />
  </div>
</template>

<script lang="ts">
import { Vue, Component } from 'vue-property-decorator'
import CheckboxGroup from './checkBoxGroup.vue'

@Component({
  components: { CheckboxGroup },
})
export default class Index extends Vue {

  // This list would usually come from an API
  allDays = ['Monday', 'Tuesday', 'Wednesday']

  checkedDays = ['Monday']
}
</script>

So the code is working almost fine, but I am getting所以代码几乎可以正常工作,但我得到了

[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders... [Vue 警告]:避免直接改变 prop,因为每当父组件重新渲染时,该值都会被覆盖...

Is there any way around it?有什么办法吗? Any help would be appriciated.任何帮助都将不胜感激。

you can't mutate the parent state from the children directly, however you can emit the event from child to parent to mutate from there as below:您不能直接从子级更改父级状态,但是您可以将事件从子级发送到父级以从那里进行更改,如下所示:

 Vue.component('check-box-group', { template: ` <div> <label v-for="day in allDays" :key="day"> <input v-model="checkedDays" :value="day" @click="$emit('update-checked-days', { newCheckedDay: day })" type="checkbox" /> <span>{{ day }}</span> </label> <div>Checked days: {{ checkedDays }}</div> </div> `, props: { checkedDays: { type: Array, default: () => ([]) }, allDays: { type: Array, default: () => ([]) }, } }) new Vue({ el: "#app", data() { return { allDays: ['Monday', 'Tuesday', 'Wednesday'], checkedDays: ['Monday'] } }, methods: { HandleUpdateCheckedDays({newCheckedDay}) { const indexOfCheckedDay = this.checkedDays.findIndex(checkedDay => checkedDay === newCheckedDay) if (indexOfCheckedDay === -1) { // if not exists then add to checkedDays this.checkedDays.push(newCheckedDay) } else { this.checkedDays = this.checkedDays.filter((_, i) => i !== indexOfCheckedDay) } } } })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.11/vue.min.js"></script> <div id="app"> <check-box-group :checked-days="checkedDays" :all-days="allDays" @update-checked-days="HandleUpdateCheckedDays" /> </div>

note : remember that TS class composition is deprecated.注意:请记住,TS 类组合已被弃用。

Thanks for the answer, I actually managed to solve it also with v-model, but it looks kind of hacky and not very reusable for cases if data is injected from the outside Models.感谢您的回答,我实际上也设法使用 v-model 解决了它,但是如果数据是从外部模型注入的,它看起来有点 hacky 并且不太可重用。 So I will go with your solution.所以我会选择你的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM