如何在VueJs中添加动态属性

Mar*_*inh 35 javascript dynamic vue.js vue-component

我正在使用vuejs,我想知道如何控制输入(必要时添加禁用属性).有没有办法在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>
Run Code Online (Sandbox Code Playgroud)

用法:

<textfield placeholder="Name" value.sync="el.name" :disabled="true"></textfield>
Run Code Online (Sandbox Code Playgroud)

Jef*_*eff 40

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

<textfield label="Name" value.sync="el.name" :disabled="myVar">
Run Code Online (Sandbox Code Playgroud)

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

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

<template>
  <input type="text" :disabled="disabled" placeholder="{{ placeholder }}" v-model="value">
</template>
Run Code Online (Sandbox Code Playgroud)

  • 请注意,在最新版本的 Vue 中,占位符也需要为 `:placeholder="placeholder"` (2认同)

ros*_*.dk 24

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

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

例子:

<a v-bind:[attributeName]="whatever">
Run Code Online (Sandbox Code Playgroud)

它记录在这里


Bla*_*rse 5

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

我要展示的内容:

例子

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

    • 第一个 div 的输入将小写转换为大写。
    • 第二个代表心情,如果输入“高兴”,则表示“好”。如果输入“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;
    }
    
    Run Code Online (Sandbox Code Playgroud)