vue :style :class多个三元表达式判断使用
warning:
这篇文章距离上次修改已过193天,其中的内容可能已经有所变动。
在Vue中,你可以在:style
和:class
绑定中使用多个三元表达式来动态地应用样式和类。这里是一个简单的例子:
<template>
<div :class="{ active: isActive, 'text-success': hasSuccess, 'text-danger': hasError }"
:style="{ color: hasError ? 'red' : hasSuccess ? 'green' : 'black', fontWeight: isBold ? 'bold' : 'normal' }">
Hello Vue!
</div>
</template>
<script>
export default {
data() {
return {
isActive: true,
hasSuccess: true,
hasError: false,
isBold: true
};
}
};
</script>
在这个例子中,:class
绑定根据数据属性isActive
、hasSuccess
和hasError
来决定应用哪些类。同时,:style
绑定根据hasError
和hasSuccess
的值决定color
和fontWeight
的值。如果hasError
为true
,则字体颜色为红色;如果hasSuccess
为true
,则字体颜色为绿色;否则字体颜色为黑色。如果isBold
为true
,则fontWeight
为粗体,否则为正常。
评论已关闭