使⽤Vue绑定单个或多个Class名的实例代码⼀、⽤变量形式绑定单个 Class 名
在 vue 中绑定单个 class 名还好说,直接写就可以了
<template>
<div id="app">
<!-- 因为是⾃定义属性,所以要⽤到 v-bind ,简写为 : -->
<!-- 3.将 box 绑定给 div -->
<div :class="box"></div>
</div>
</template>
<script>
export default {
name: 'app',
data () {
return {
// 2.在 data 中把 yellow 赋给 box
box: 'yellow'
}
}
}
</script>
<style>
/* 1.在样式表中写好样式 */
.
yellow{
width: 200px;
height: 200px;
background: #ff0;
}
</style>
⽤ Vue 绑定单个Class 名
⼆、⽤数组形式绑定多个 Class 名
⽐如我们想再给这个 div 加个阴影
在 style 中写好样式
vue json字符串转数组
.shadow{
box-shadow: 10px 10px 5px 0 #999;
}
在 data 中继续添加数据对象
<script>
export default {
name: 'app',
data () {
return {
box: 'yellow',
shadow:'shadow'
}
}
}
</script>
在标签中以数组的形式绑定 Class 名
<template>
<div id="app">
<div :class="[box,shadow]"></div>
</div>
</template>
就 OK 了。
⽤数组形式绑定多个 Class 名
三、⽤ json 形式绑定多个 Class 名
该⽅法⽅便⽤于当同时添加多个 Class 名时,在某种情况下判断显⽰哪种样式
先写好样式
<style>
.yellow{
width: 200px;
height: 200px;
background: #ff0;
}
.shadow{
box-shadow: 10px 10px 5px 0 #999;
}
</style>
在 data 中添加数字对象,⽤来做判断
<script>
export default {
name: 'app',
data () {
return {
show1:true,
show2:false
}
}
}
</script>
以 json 的形式绑定到 class 中,通过布尔值改变 show1 与 show2 的值,来控制 div 的状态
<template>
<div id="app">
<div :class="{yellow:show1,shadow:show2}"></div>
</div>
</template>
⽤ json 形式绑定多个 Class
ps:vue解决跨域问题
改config/index.js⽂件
proxyTable: {
// 请求到 '/device' 下的请求都会被代理到 target: 中
'/v1/*': {
target: 'api.tiaotiao5',
secure: true, // 接受运⾏在 https 上的服务
changeOrigin: true
}
}
总结
以上所述是⼩编给⼤家介绍的使⽤ Vue 绑定单个或多个 Class 名的实例代码,希望对⼤家有所帮助,如果⼤家有任何疑问请给我留⾔,⼩编会及时回复⼤家的。在此也⾮常感谢⼤家对⽹站的⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。