我正在使用vuejs,我想知道如何控制输入(必要时添加禁用属性)。有没有办法在vuejs中添加动态属性?在我的文本字段组件下面:
<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>
答案 0 :(得分:35)
您可以使用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>
答案 1 :(得分:1)
在尝试使用Vue v-for循环时,如何从数组值中动态设置html标签的属性。
我要显示的内容:
每个div都有一个输入标签,并在用户输入值时更改值
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;
}
答案 2 :(得分:0)
我们可以在vue中定义或更改属性的一个条件
请参考正式文件,以获取相同的https://vuejs.org/v2/guide/syntax.html#Attributes
答案 3 :(得分:0)
正如所指出的,您的情况不需要动态属性。
不过,你问是否有可能,答案是肯定的。从 2.6.0 开始,您可以拥有动态属性。
示例:
<a v-bind:[attributeName]="whatever">
已记录here