默认情况下使用 v-model 选中复选框

By default Checked checkbox using v-model

大家好,我是 VueJS 的新手, 任何可以帮助我在我的复选框列表中进行默认检查的人

我正在使用 v-model,所以 checked 无法使用

有什么想法或建议吗?

您使用 v-model 指令将输入绑定到真值

<input type="checkbox" v-model="selected">

在您的组件数据中(根据@Phil 的评论更新)

data () {
  return {
   selected: true, // True of false, or other truthy value depending on needs
  }
}

然后,根据所选的值,您的输入将为 checked/unchecked。

注意 - 在文档中指出

v-model will ignore the initial value, checked, or selected attributes found on any form elements. It will always treat the Vue instance data as the source of truth. You should declare the initial value on the JavaScript side, inside the data option of your component.

https://vuejs.org/v2/guide/components-custom-events.html#Binding-Native-Events-to-Components

阅读更多内容