Vue3 | Vue中的诸多花样样式写法 以及 相关规则和技巧

完整原文地址见简书https://www.jianshu.com/p/cdbd2670e075

本文内容提要

  • Class样式写法

    • 常规的样式使用写法
    • 使用v-bind的形式动态设定DOM组件样式
    • 使用v-bind + Object的形式组织样式 绑定DOM组件;
    • 使用v-bind + 数组的形式组织样式 绑定DOM组件
    • 数组形式中混合Object形式的;
  • 子组件样式 默认跟随 父组件

    • 子组件自己配置样式,则不跟随根组件,按子组件自己的样式渲染
    • 拥有“两个以上最外层组件”的样式处理
      • 解决办法1,外层组件 各自配置样式
      • 解决办法2,使用:class="$attrs.class"对外层组件进行配置,
        使得统一跟随引用处样式配置;
  • 行内样式写法

    • 常规写法
    • Vue式写法,使用v-bind配合data
    • Object形式描述样式,可读性更高
Class样式写法

常规的样式使用写法:

<!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>
Object形式描述样式

当然以上是string方式描述样式的方式,
更多时候我们使用Object的形式描述样式,可读性更高
如下例程,styleStringstyleObject两个字段,
分别代表以上两种描述方式,相形见绌:

<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>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凌川江雪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值