Vue3 | Vue中的諸多花樣樣式寫法 以及 相關(guān)規(guī)則和技巧

完整原文地址見(jiàn)簡(jiǎn)書http://www.reibang.com/p/cdbd2670e075

更多完整Vue筆記目錄敬請(qǐng)見(jiàn)《前端 Web 筆記 匯總目錄(Updating)》



本文內(nèi)容提要

  • Class樣式寫法

    • 常規(guī)的樣式使用寫法
    • 使用v-bind的形式動(dòng)態(tài)設(shè)定DOM組件樣式
    • 使用v-bind + Object的形式組織樣式 綁定DOM組件捡鱼;
    • 使用v-bind + 數(shù)組的形式組織樣式 綁定DOM組件
    • 數(shù)組形式中混合Object形式的蝉衣;
  • 子組件樣式 默認(rèn)跟隨 父組件

    • 子組件自己配置樣式彪腔,則不跟隨根組件,按子組件自己的樣式渲染
    • 擁有“兩個(gè)以上最外層組件”的樣式處理
      • 解決辦法1帮孔,外層組件 各自配置樣式
      • 解決辦法2不撑,使用:class="$attrs.class"對(duì)外層組件進(jìn)行配置文兢,
        使得統(tǒng)一跟隨引用處樣式配置;
  • 行內(nèi)樣式寫法

    • 常規(guī)寫法
    • Vue式寫法焕檬,使用v-bind配合data
    • Object形式描述樣式姆坚,可讀性更高


Class樣式寫法

常規(guī)的樣式使用寫法:

<!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的形式動(dòng)態(tài)設(shè)定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字段可以動(dòng)態(tài)改變組件顏色:



使用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>

關(guān)鍵代碼:

 data() {
            return {
                colorObject: {blue:true, green:true}
            }
        },

效果如下:

如果將顏色鍵值設(shè)置成false,則網(wǎng)頁(yè)DOM組件便不會(huì)展示:

<script>
    const app = Vue.createApp({
        data() {
            return {
                colorObject: {blue:true, green:false}
            }
        },
        template: `
        <div :class = "colorObject">luelueluelielielie</div>`
    });
    const vm = app.mount('#heheApp');
  </script>

效果:



使用數(shù)組的形式組織樣式 綁定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>

效果:



數(shù)組形式中混合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>

效果:


子組件樣式 默認(rèn)跟隨 父組件

例程:
添加子組件testDom到根組件实愚,子組件樣式?jīng)]有配置兼呵,則默認(rèn)跟隨根組件:

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

運(yùn)行效果:



子組件自己配置樣式,則不跟隨根組件腊敲,按子組件自己的樣式渲染:

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


擁有“兩個(gè)以上最外層組件”的樣式處理

不過(guò)當(dāng)添加的子組件的template中击喂,最外層有兩個(gè)以上的組件的時(shí)候,
在引用子組件處(如下代碼中的<testDom class='green'/>)配置樣式是沒(méi)有作用碰辅,
子組件template下的組件 會(huì)沿用根組件的樣式(如下代碼中的<div :class = "colorArray">)懂昂,

因?yàn)橐锰?code><testDom class='green'/>配置的樣式 或者其他屬性,
指代的是testDom組件的最外層組件的樣式 或者其他屬性乎赴,
但是此時(shí)最外層組件有兩個(gè)忍法,
于是這個(gè)樣式class='green'配置不知道該分配給哪個(gè)最外層組件,便失效:

  <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"對(duì)外層組件進(jìn)行配置,
將自定義子組件 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>

運(yùn)行效果:


行內(nèi)樣式寫法

常規(guī)寫法:

<!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,
老規(guī)矩顽素,bind后接左邊一個(gè)組件屬性style咽弦,右邊一個(gè)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形式描述樣式

當(dāng)然以上是string方式描述樣式的方式,
更多時(shí)候我們使用Object的形式描述樣式胁出,可讀性更高
如下例程型型,
styleStringstyleObject兩個(gè)字段,
分別代表以上兩種描述方式全蝶,相形見(jiàn)絀:

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

效果:
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末闹蒜,一起剝皮案震驚了整個(gè)濱河市寺枉,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌绷落,老刑警劉巖姥闪,帶你破解...
    沈念sama閱讀 217,185評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異砌烁,居然都是意外死亡筐喳,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門函喉,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)避归,“玉大人,你說(shuō)我怎么就攤上這事管呵』痹啵” “怎么了?”我有些...
    開(kāi)封第一講書人閱讀 163,524評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵撇寞,是天一觀的道長(zhǎng)顿天。 經(jīng)常有香客問(wèn)我,道長(zhǎng)蔑担,這世上最難降的妖魔是什么牌废? 我笑而不...
    開(kāi)封第一講書人閱讀 58,339評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮啤握,結(jié)果婚禮上鸟缕,老公的妹妹穿的比我還像新娘。我一直安慰自己排抬,他們只是感情好懂从,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,387評(píng)論 6 391
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著蹲蒲,像睡著了一般番甩。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上届搁,一...
    開(kāi)封第一講書人閱讀 51,287評(píng)論 1 301
  • 那天缘薛,我揣著相機(jī)與錄音,去河邊找鬼卡睦。 笑死宴胧,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的表锻。 我是一名探鬼主播恕齐,決...
    沈念sama閱讀 40,130評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼瞬逊!你這毒婦竟也來(lái)了显歧?” 一聲冷哼從身側(cè)響起补胚,我...
    開(kāi)封第一講書人閱讀 38,985評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎追迟,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體骚腥,經(jīng)...
    沈念sama閱讀 45,420評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡敦间,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,617評(píng)論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了束铭。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片廓块。...
    茶點(diǎn)故事閱讀 39,779評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖契沫,靈堂內(nèi)的尸體忽然破棺而出带猴,到底是詐尸還是另有隱情,我是刑警寧澤懈万,帶...
    沈念sama閱讀 35,477評(píng)論 5 345
  • 正文 年R本政府宣布拴清,位于F島的核電站,受9級(jí)特大地震影響会通,放射性物質(zhì)發(fā)生泄漏口予。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,088評(píng)論 3 328
  • 文/蒙蒙 一涕侈、第九天 我趴在偏房一處隱蔽的房頂上張望沪停。 院中可真熱鬧,春花似錦裳涛、人聲如沸木张。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 31,716評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)舷礼。三九已至,卻和暖如春郊闯,著一層夾襖步出監(jiān)牢的瞬間且轨,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 32,857評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工虚婿, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留旋奢,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,876評(píng)論 2 370
  • 正文 我出身青樓然痊,卻偏偏與公主長(zhǎng)得像至朗,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子剧浸,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,700評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容