VueJS有条件地添加元素的属性 [英] VueJS conditionally add an attribute for an element

查看:1400
本文介绍了VueJS有条件地添加元素的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VueJS中,我们可以使用v-if添加或删除DOM元素:

In VueJS we can add or remove a DOM element using v-if:

<button v-if="isRequired">Important Button</button>

但有没有办法添加/删除dom元素的属性,例如以下条件设置必需属性:

but is there a way to add / remove attributes of a dom element eg for the following conditionally set the required attribute:

Username: <input type="text" name="username" required>

类似于:

Username: <input type="text" name="username" v-if="name.required" required>

任何想法?

推荐答案

尝试:

<input :required="test ? true : false">

这篇关于VueJS有条件地添加元素的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆