如何在VueJs中动态添加属性

JavaScript

小小理查德

2020-03-11

我正在使用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>

用法

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

第756篇《如何在VueJs中动态添加属性》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

3个回答
神奇飞云 2020.03.11

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

我要显示的是:

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

    • 第一个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;
    }
    
猴子神乐 2020.03.11

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

请参考官方文档以获取相同的https://vuejs.org/v2/guide/syntax.html#Attributes

SamStafan十三 2020.03.11

您可以使用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>

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android