完整原文地址见简书https://cloud.tencent.com/developer/article/1795155
- **`常规`****的样式使用写法**
- **使用****`v-bind`****的形式动态设定DOM组件样式**
- **使用****`v-bind`** **+** **`Object`****的形式组织样式 绑定DOM组件;**
- **使用****`v-bind`** **+** **`数组`****的形式组织样式 绑定DOM组件**
- **`数组`****形式中混合****`Object`****形式的;**
- **子组件自己配置样式,则不跟随根组件,按子组件自己的样式渲染**
- **拥有“两个以上最外层组件”的样式处理**
- **解决办法1,****`外层组件 各自配置样式`****;**
- **解决办法2,使用****`:class="$attrs.class"`****对外层组件进行配置,**
- **`常规`****写法**
- **Vue式写法,使用****`v-bind`****配合data**
- **`Object`****形式描述样式,可读性更高**
常规的样式使用写法:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello World! heheheheheheda</title>
<style>
.blue {
color: blue;
}
.green {
color: green;
}
</style>
<script src="https://unpkg.com/vue@next"></script>
</head>
<body>
<div id="heheApp"></div>
</body>
<script>
const app = Vue.createApp({
template: `
<div class = "blue">luelueluelielielie</div>`
});
const vm = app.mount('#heheApp');
</script>
</html>
效果:
使用**v-bind
**的形式动态设定DOM组件样式:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello World! heheheheheheda</title>
<style>
.blue {
color: blue;
}
.green {
color: green;
}
</style>
<script src="https://unpkg.com/vue@next"></script>
</head>
<body>
<div id="heheApp"></div>
</body>
<script>
const app = Vue.createApp({
data() {
return {
colorString:'blue',
}
},
template: `
<div :class = "colorString">luelueluelielielie</div>`
});
const vm = app.mount('#heheApp');
</script>
</html>
改变**data字段
**可以动态改变组件颜色:
使用**Object
**的形式组织样式 绑定DOM组件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello World! heheheheheheda</title>
<style>
.blue {
color: blue;
}
.green {
color: green;
}
</style>
<script src="https://unpkg.com/vue@next"></script>
</head>
<body>
<div id="heheApp"></div>
</body>
<script>
const app = Vue.createApp({
data() {
return {
colorObject: {blue:true, green:true}
}
},
template: `
<div :class = "colorObject">luelueluelielielie</div>`
});
const vm = app.mount('#heheApp');
</script>
</html>
关键代码:
data() {
return {
colorObject: {blue:true, green:true}
}
},
效果如下:
如果将颜色键值设置成false
,则网页DOM组件便不会展示:
<script>
const app = Vue.createApp({
data() {
return {
colorObject: {blue:true, green:false}
}
},
template: `
<div :class = "colorObject">luelueluelielielie</div>`
});
const vm = app.mount('#heheApp');
</script>
效果:
使用**数组
**的形式组织样式 绑定DOM组件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello World! heheheheheheda</title>
<style>
.blue {
color: blue;
}
.green {
color: green;
}
.orange {
color: orange;
}
.yellow {
color: yellow;
}
</style>
<script src="https://unpkg.com/vue@next"></script>
</head>
<body>
<div id="heheApp"></div>
</body>
<script>
const app = Vue.createApp({
data() {
return {
colorArray: ['blue', 'green', 'orange', 'yellow']
}
},
template: `
<div :class = "colorArray">luelueluelielielie</div>`
});
const vm = app.mount('#heheApp');
</script>
</html>
效果:
数组
**形式中混合**Object
**形式的:**
<script>
const app = Vue.createApp({
data() {
return {
colorArray: ['blue', 'green', {orange:false, yellow:true}]
}
},
template: `
<div :class = "colorArray">luelueluelielielie</div>`
});
const vm = app.mount('#heheApp');
</script>
效果:
例程:
添加子组件testDom
到根组件,子组件样式没有配置,则默认跟随根组件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello World! heheheheheheda</title>
<style>
.blue {
color: blue;
}
.green {
color: green;
}
.orange {
color: orange;
}
</style>
<script src="https://unpkg.com/vue@next"></script>
</head>
<body>
<div id="heheApp"></div>
</body>
<script>
const app = Vue.createApp({
data() {
return {
colorArray: ['blue', 'green', {orange:true}]
}
},
template: `
<div :class = "colorArray">
luelueluelielielie
<testDom/>
</div>`
});
app.component('testDom', {
template: `
<div>heheda</div>`
});
const vm = app.mount('#heheApp');
</script>
</html>
运行效果:
子组件自己配置样式,则不跟随根组件,按子组件自己的样式渲染:
<script>
const app = Vue.createApp({
data() {
return {
colorArray: ['blue', 'green', {orange:true}]
}
},
template: `
<div :class = "colorArray">
luelueluelielielie
<testDom/>
</div>`
});
app.component('testDom', {
template: `
<div class='blue'>heheda</div>`
});
const vm = app.mount('#heheApp');
</script>
不过当添加的子组件的**template
**中,最外层有两个以上的组件的时候,
在引用子组件处(如下代码中的**<testDom class='green'/>
**)配置样式是没有作用,
子组件**template
**下的组件 会沿用根组件的样式(如下代码中的**<div :class = "colorArray">
**),
因为引用处**<testDom class='green'/>
**配置的样式 或者其他属性,
指代的是**testDom
**组件的**最外层组件
**的样式 或者其他属性,
但是此时**最外层组件
**有两个,
于是这个样式**class='green'
**配置不知道该分配给哪个**最外层组件
**,便失效:
<script>
const app = Vue.createApp({
data() {
return {
colorArray: ['blue', 'green', {orange:true}]
}
},
template: `
<div :class = "colorArray">
luelueluelielielie
<testDom class='green'/>
</div>`
});
app.component('testDom', {
template: `
<div>heheda</div>
<div>heheda</div>
`
});
const vm = app.mount('#heheApp');
</script>
解决办法1,各自配置样式:
<script>
const app = Vue.createApp({
data() {
return {
colorArray: ['blue', 'green', {orange:true}]
}
},
template: `
<div :class = "colorArray">
luelueluelielielie
<testDom/>
</div>`
});
app.component('testDom', {
template: `
<div class='blue'>heheda</div>
<div class='green'>heheda</div>
`
});
const vm = app.mount('#heheApp');
</script>
解决办法2,使用**:class="$attrs.class"
**对外层组件进行配置,
将自定义子组件 template下的组件 的样式,
跟随 子组件添加处(如下代码中的**<testDom class='green'/>
**)配置的样式:
<script>
const app = Vue.createApp({
data() {
return {
colorArray: ['blue', 'green', {orange:true}]
}
},
template: `
<div :class = "colorArray">
luelueluelielielie
<testDom class='blue'/>
</div>`
});
app.component('testDom', {
template: `
<div :class="$attrs.class">heheda</div>
<div :class="$attrs.class">heheda</div>
`
});
const vm = app.mount('#heheApp');
</script>
运行效果:
常规写法:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello World! heheheheheheda</title>
<style>
.blue {
color: blue;
}
.green {
color: green;
}
.orange {
color: orange;
}
</style>
<script src="https://unpkg.com/vue@next"></script>
</head>
<body>
<div id="heheApp"></div>
</body>
<script>
const app = Vue.createApp({
template: `
<div style="color:blue">
luelueluelielielie
</div>`
});
const vm = app.mount('#heheApp');
</script>
</html>
效果:
Vue式写法,使用**v-bind
**配合data,
老规矩,bind后接左边一个组件属性**style
**,右边一个data字段**styleString
**:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello World! heheheheheheda</title>
<style>
.blue {
color: blue;
}
.green {
color: green;
}
.orange {
color: orange;
}
</style>
<script src="https://unpkg.com/vue@next"></script>
</head>
<body>
<div id="heheApp"></div>
</body>
<script>
const app = Vue.createApp({
data() {
return {
styleString:'color:blue;'
}
},
template: `
<div :style="styleString">
luelueluelielielie
</div>`
});
const vm = app.mount('#heheApp');
</script>
</html>
效果一样:
当然以上是**string
**方式描述样式的方式,
更多时候我们使用Object的形式描述样式,可读性更高
如下例程,
styleString
**和**styleObject
**两个字段,**
分别代表以上两种描述方式,相形见绌:
<script>
const app = Vue.createApp({
data() {
return {
styleString:'color:blue; background: orange',
styleObject: {
color: 'blue',
background:'orange'
}
}
},
template: `
<div :style="styleObject">
luelueluelielielie
</div>`
});
const vm = app.mount('#heheApp');
</script>
效果: