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

40 阅读1分钟

#完整原文地址见简书www.jianshu.com/p/cdbd2670e… #更多完整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样式写法 **常规的样式使用写法:** ``` Hello World! heheheheheheda .blue { color: blue; } .green { color: green; }
**效果:![](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/83fd1e6897d248f1bcdc36854ba3fe90~tplv-k3u1fbpfcp-zoom-1.image)** 

<br>
**使用`v-bind`的形式动态设定DOM组件样式:**
Hello World! heheheheheheda .blue { color: blue; } .green { color: green; }
``` ![](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/2325adb9d535473eb832833e126aa746~tplv-k3u1fbpfcp-zoom-1.image)**改变`data字段`可以动态改变组件颜色:**![](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/c7fbeb5e30bd43a2823409ecf6122196~tplv-k3u1fbpfcp-zoom-1.image)
**使用`Object`的形式组织样式 绑定DOM组件:** ``` Hello World! heheheheheheda .blue { color: blue; } .green { color: green; }
``` 关键代码: ``` data() { return { colorObject: {blue:true, green:true} } }, ``` 效果如下:![](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/9bac71da581d413abcefc0862e643ef6~tplv-k3u1fbpfcp-zoom-1.image)

如果将颜色键值设置成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组件:** ``` Hello World! heheheheheheda .blue { color: blue; } .green { color: green; } .orange { color: orange; } .yellow { color: yellow; }
``` 效果:![](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/eb33612ab4d44407933ee28daa6c53fa~tplv-k3u1fbpfcp-zoom-1.image)
**`数组`形式中混合`Object`形式的:** ``` ``` 效果:![](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/5920ab35029640288de505561844dcaa~tplv-k3u1fbpfcp-zoom-1.image)
#####子组件样式 默认跟随 父组件 例程: 添加子组件`testDom`到根组件,子组件样式没有配置,则默认跟随根组件: ``` Hello World! heheheheheheda .blue { color: blue; } .green { color: green; } .orange { color: orange; }
``` 运行效果:![](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/39a59adc0fbc40d6ae30824a7c243371~tplv-k3u1fbpfcp-zoom-1.image)
**子组件自己配置样式,则不跟随根组件,按子组件自己的样式渲染:** ``` ``` ![](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/92d178ca7a004deea932a81458211e40~tplv-k3u1fbpfcp-zoom-1.image)
####拥有“两个以上最外层组件”的样式处理 **不过当添加的子组件的`template`中,最外层有两个以上的组件的时候, 在引用子组件处(如下代码中的``)配置样式是没有作用, 子组件`template`下的组件 会沿用根组件的样式(如下代码中的`
`),
因为引用处``配置的样式 或者其他属性, 指代的是`testDom`组件的`最外层组件`的样式 或者其他属性, 但是此时`最外层组件`有两个, 于是这个样式`class='green'`配置不知道该分配给哪个`最外层组件`,便失效:** ``` ``` ![](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/3f9e41278b0e48c689ceee74af666813~tplv-k3u1fbpfcp-zoom-1.image)
**解决办法1,各自配置样式:** ``` ``` ![](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/56e585ab91e24c9b9ccae0f239987ecc~tplv-k3u1fbpfcp-zoom-1.image)
**解决办法2,使用`:class="$attrs.class"`对外层组件进行配置, 将自定义子组件 template下的组件 的样式, 跟随 子组件添加处(如下代码中的``)配置的样式:** ``` ``` 运行效果:![](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/6c47adedf8b345ed963d0764df20e063~tplv-k3u1fbpfcp-zoom-1.image)
####行内样式写法 **常规写法:** ``` Hello World! heheheheheheda .blue { color: blue; } .green { color: green; } .orange { color: orange; }
``` 效果:![](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/499342b1ce824b1287d5e2a741c72968~tplv-k3u1fbpfcp-zoom-1.image)
**Vue式写法,使用`v-bind`配合data, 老规矩,bind后接左边一个组件属性`style`,右边一个data字段`styleString`:** ``` Hello World! heheheheheheda .blue { color: blue; } .green { color: green; } .orange { color: orange; }
``` 效果一样:![](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/b23286b5e16e481d809dcf5ad5d725ac~tplv-k3u1fbpfcp-zoom-1.image)
#####Object形式描述样式 **当然以上是`string`方式描述样式的方式, 更多时候我们使用Object的形式描述样式,可读性更高 如下例程, `styleString`和`styleObject`两个字段, 分别代表以上两种描述方式,相形见绌:** ``` ``` 效果:![](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/b6437df3fc904254b9dc169f7c36771c~tplv-k3u1fbpfcp-zoom-1.image)