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

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

更多完整Vue笔记目录敬请见《前端 Web 笔记 汇总目录(Updating)》



本文内容提要

  • 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>

效果:
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容