前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Vue3 | Vue中的诸多花样样式写法 以及 相关规则和技巧

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

作者头像
凌川江雪
发布2021-03-02 10:59:22
8520
发布2021-03-02 10:59:22
举报
文章被收录于专栏:李蔚蓬的专栏李蔚蓬的专栏

完整原文地址见简书https://cloud.tencent.com/developer/article/1795155

本文内容提要

  • Class样式写法
代码语言:txt
复制
- **`常规`****的样式使用写法**
- **使用****`v-bind`****的形式动态设定DOM组件样式**
- **使用****`v-bind`** **+** **`Object`****的形式组织样式 绑定DOM组件;**
- **使用****`v-bind`** **+** **`数组`****的形式组织样式 绑定DOM组件**
- **`数组`****形式中混合****`Object`****形式的;**
代码语言:txt
复制
- **子组件自己配置样式,则不跟随根组件,按子组件自己的样式渲染**
-  **拥有“两个以上最外层组件”的样式处理** 
    - **解决办法1,****`外层组件 各自配置样式`****;**
    - **解决办法2,使用****`:class="$attrs.class"`****对外层组件进行配置,** 
代码语言:txt
复制
- **`常规`****写法**
- **Vue式写法,使用****`v-bind`****配合data**
- **`Object`****形式描述样式,可读性更高**
Class样式写法

常规的样式使用写法:

代码语言:javascript
复制
<!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组件样式:

代码语言:javascript
复制
<!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组件:

代码语言:javascript
复制
<!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>

关键代码:

代码语言:javascript
复制
 data() {
            return {
                colorObject: {blue:true, green:true}
            }
        },

效果如下:

如果将颜色键值设置成false,则网页DOM组件便不会展示:

代码语言:javascript
复制
<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组件:

代码语言:javascript
复制
<!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**形式的:**

代码语言:javascript
复制
<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到根组件,子组件样式没有配置,则默认跟随根组件:

代码语言:javascript
复制
<!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>

运行效果:

子组件自己配置样式,则不跟随根组件,按子组件自己的样式渲染:

代码语言:javascript
复制
  <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'**配置不知道该分配给哪个**最外层组件**,便失效:

代码语言:javascript
复制
  <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,各自配置样式:

代码语言:javascript
复制
  <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'/>**)配置的样式:

代码语言:javascript
复制
  <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>

运行效果:

行内样式写法

常规写法:

代码语言:javascript
复制
<!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**:

代码语言:javascript
复制
<!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的形式描述样式,可读性更高

如下例程,

styleString**和**styleObject**两个字段,**

分别代表以上两种描述方式,相形见绌:

代码语言:javascript
复制
<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>

效果:

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 本文内容提要
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档