【问题标题】:How to add dynamically attribute in VueJs如何在 VueJs 中动态添加属性
【发布时间】:2017-01-07 21:56:44
【问题描述】:

我正在使用 vuejs,我想知道如何控制输入(必要时添加 disabled 属性)。有没有办法在 vuejs 中添加动态属性?在我的 Textfield 组件 下方:

    <template>
     <input type="text" placeholder="{{ placeholder }}" v-model="value">
    </template>
    <script>
    export default  {
      props: {
       disabled: {type: Boolean, default: false},
       placeholder: {type: String, default: ""},
       value: {twoWay: true, default: ""}
      }
     }
    </script>

用法

<textfield placeholder="Name" value.sync="el.name" :disabled="true"></textfield>

【问题讨论】:

    标签: javascript vue.js dynamic vue-component


    【解决方案1】:

    您可以使用v-bind:disabled="foo" 或简称:disabled="foo" 将其绑定到变量:

    <textfield label="Name" value.sync="el.name" :disabled="myVar">
    

    然后在 Vue 中你可以设置this.myVar = true,它会禁用输入。

    编辑:将其添加到您的模板中:

    <template>
      <input type="text" :disabled="disabled" :placeholder="placeholder" v-model="value">
    </template>
    

    【讨论】:

    • 请注意,在最新版本的 Vue 中,占位符也需要是 :placeholder="placeholder"
    【解决方案2】:

    正如所指出的,您的情况不需要动态属性。

    但是,您问是否可能,答案是肯定的。从 2.6.0 开始,您可以拥有动态属性。

    例子:

    <a v-bind:[attributeName]="whatever">
    

    记录在案here

    【讨论】:

      【解决方案3】:

      基于一个条件,我们可以在 vue 中定义或更改属性

      请参考官方文档https://vuejs.org/v2/guide/syntax.html#Attributes

      【讨论】:

      • 请添加示例,而不是链接
      • 链接后面有一个很好的例子,谢谢你的指点。我不知何故错过了它
      • 如果有数组 [1,2,3,4],您可以添加类似 v-for="(item, index) in numbers" :mycustom="item" 的属性
      【解决方案4】:

      我试图弄清楚在使用Vue v-for循环时如何从数组值中动态设置html标签的属性。

      我要展示的内容:

      Example

      1. 数组值中有 3 个具有不同背景颜色的 div 元素(非静态)。
      2. 每个div都有一个输入标签,当用户输入值时改变值

        • 第一个 div 的输入将小写转换为大写。
        • second代表心情,如果输入'happy',则呈现'good'。如果输入'sad',输出'bad'
        • 第三个 div 输入将输入值加倍。
        {{ box.outputData }} 圆框
        new Vue({
         el: "#app",
          data: {
            isRounded: false,
              boxes: [
                {
                  inputData: "",
                  outputData: "",
                  color: "green",
                  operation: "uppercase"
                },
                {
                  inputData: "",
                  outputData: "",
                  color: "red",
                  operation: "feeling"
                },
                {
                  inputData: "",
                  outputData: "",
                  color: "blue",
                  operation: "multiple"
                }
              ],
              feeling: {
                good: ["happy", "joyful", "calm"],
                bad: ["sad", "mad", "depressed"]
              }
          },
          methods: {
            toggle: function(todo){
                todo.done = !todo.done
            }
          },
          watch: {
            boxes: {
              deep: true,
              immediate: true,
              handler: function(val) {
                this.boxes.map(box => {
                  if (box.operation === "uppercase")
                    box.outputData = box.inputData.toUpperCase();
                  else if (box.operation === "feeling") {
                    box.outputData = this.feeling.good.includes(box.inputData)
                      ? "GOOD"
                      : this.feeling.bad.includes(box.inputData)
                      ? "BAD"
                      : "";
                  } else if (box.operation === "multiple") {
                    if (box.inputData == "") box.outputData = "";
                    else box.outputData = parseInt(box.inputData) * 2;
                  }
                });
              }
            }
          },
          mounted() {
            for (var i = 0; i < this.numBox; i++) {
              this.boxValue[i] = "";
              this.bxData[i] = "";
            }
          },
        })
        
        
        
        .clearfix{
         clear: both;
        }
        .full-width{
          width:100%;
        }
        input {
          background: transparent;
          text-decoration: underline;
          color: white;
          border: none;
          text-align: center;
          font-size:30px;
        }
        .box {
          float:left;
          color: white;
          width: 24%;
          margin-right: 1%;
          padding: 20px;
          background: blue;
          height: 100px;
        }
        .box-output {
          width: 100%;
          text-align: center;
          font-size:30px;
        }
        .box-rounded {
          border-radius: 50px;
        }
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-31
        • 2018-02-10
        • 2017-07-03
        • 2011-12-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多