小编Dha*_*eda的帖子

Vue JS 中复选框的适当两种方式绑定

我从 MySQL 数据库获取数据,其形式为“1”和“0”,分别代表布尔值 true 和 false。这些值按以下方式在 vue 组件中设置:

    data(){
    return {
    form : {
         attribute_1 : "1", //attribute 1 is true
         attribute_2 : "0", //attribute 2 is false
         attribute_3 : "1", //attribute 3 is true
          }
        }
       }
Run Code Online (Sandbox Code Playgroud)

为了维护双向绑定,我当前使用计算属性,如下所示:

 attribute1: {
            get(){
                return this.form.attribute_1 == "1" ? true : false ;
            },
            set(newValue){
                this.form.attribute_1 = newValue ? "1" : "0";
            }
        },
 attribute2: {
            get(){
                return this.form.attribute_2 == "1" ? true : false ;
            },
            set(newValue){
                this.form.attribute_2 = newValue ? …
Run Code Online (Sandbox Code Playgroud)

javascript vue.js vuejs2

3
推荐指数
1
解决办法
4532
查看次数

标签 统计

javascript ×1

vue.js ×1

vuejs2 ×1