欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue 中使用 CSS Modules優(yōu)雅方法

 更新時(shí)間:2018年04月09日 11:11:16   作者:fjc0k  
這篇文章主要介紹了Vue 中使用 CSS Modules優(yōu)雅方法,本文文字結(jié)合實(shí)例代碼的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下

CSS Modules:局部作用域 & 模塊化

CSS Modules 為每一個(gè)局部類賦予全局唯一的類名,這樣組件樣式間就不會(huì)相互影響了。如:

/* button.css */
.button {
 font-size: 16px;
}
.mini {
 font-size: 12px;
}

它會(huì)被轉(zhuǎn)換為類似這樣:

/* button.css */
.button__button--d8fj3 {
 font-size: 16px;
}
.button__mini--f90jc {
 font-size: 12px;
}

當(dāng)導(dǎo)入一個(gè) CSS 模塊文件時(shí),它會(huì)將局部類名到全局類名的映射對象提供給我們。就像這樣:

import styles from './button.css'
// styles = {
// button: 'button__button--d8fj3',
// mini: 'button__mini--f90jc'
// }
element.innerHTML = '<button class="' + styles.button + ' ' + styles.mini + '" />'

vue-css-modules :簡化類名映射

下面是一個(gè)使用了 CSS Modules 的按鈕組件:

<template>
 <button :class="{
 'global-button-class-name': true,
 [styles.button]: true,
 [styles.mini]: mini
 }">點(diǎn)我</button>
</template>
<script>
 import styles from './button.css'

 export default {
 props: { mini: Boolean },
 data: () => ({ styles })
 }
</script>

的確,CSS Modules 對于 Vue 組件是一個(gè)不錯(cuò)的選擇。但也存在以下幾點(diǎn)不足:

  • 你必須在 data 中傳入 styles
  • 你必須使用 styles.localClassName 導(dǎo)入全局類名
  • 如果有其他全局類名,你必須將它們放在一起
  • 如果要和組件的屬性值綁定,就算局部類名和屬性名一樣,也要顯式指定

對于上面的按鈕組件,使用 vue-css-modules 后:

<template>
 <button
 class="global-button-class-name"
 styleName="button :mini">
 點(diǎn)我
 </button>
</template>
<script>
 import CSSModules from 'vue-css-modules'
 import styles from './button.css'

 export default {
 mixins: [CSSModules(styles)],
 props: { mini: Boolean }
 }
</script>

現(xiàn)在:

  • 你不必在 data 中傳入 styles ,但得在 mixins 中傳入 styles :full_moon_with_face:
  • 你可以跟 styles.localClassName 說拜拜了
  • 將局部類名放在 styleName 屬性,全局類名放在 class 屬性,規(guī)整了許多
  • 局部類名綁定組件同名屬性,只需在其前面加上 : 修飾符

修飾符

@button

<button styleName="@button">按鈕</button>

這等同于:

<button styleName="button" data-component-button="true">按鈕</button>

這讓你能在外部重置組件的樣式:

.form [data-component-button] {
 font-size: 20px;
}

$type

<button styleName="$type">按鈕</button>

這等同于:

<button :styleName="type">按鈕</button>

:mini

<button styleName=":mini">按鈕</button>

這等同于:

<button :styleName="mini ? 'mini' : ''">按鈕</button>
disabled=isDisabled
<button styleName="disabled=isDisabled">按鈕</button>

這等同于:

<button :styleName="isDisabled ? 'disabled' : ''">按鈕</button>

使用方法

在 Vue 模板中使用

引入模板外部的 CSS 模塊

<template>
 <button
 class="global-button-class-name"
 styleName="button :mini">
 點(diǎn)我
 </button>
</template>
<script>
 import CSSModules from 'vue-css-modules'
 import styles from './button.css'
 export default {
 mixins: [CSSModules(styles)],
 props: { mini: Boolean }
 }
</script>

使用模板內(nèi)部的 CSS 模塊

<template>
 <button
 class="global-button-class-name"
 styleName="button :mini">
 點(diǎn)我
 </button>
</template>
<script>
 import CSSModules from 'vue-css-modules'

 export default {
 mixins: [CSSModules()],
 props: { mini: Boolean }
 }
</script>
<style module>
 .button {
 font-size: 16px;
 }
 .mini {
 font-size: 12px;
 }
</style>

在 Vue JSX 中使用

import CSSModules from 'vue-css-modules'
import styles from './button.css'
export default {
 mixins: [CSSModules(styles)],
 props: { mini: Boolean },
 render() {
 return (
  <button styleName="@button :mini">點(diǎn)我</button>
 )
 }
}

在 Vue 渲染函數(shù)中使用

import CSSModules from 'vue-css-modules'
import styles from './button.css'

export default {
 mixins: [CSSModules(styles)],
 props: { mini: Boolean },
 render(h) {
 return h('button', {
  styleName: '@button :mini'
 }, '點(diǎn)我')
 }
}

總結(jié)

以上所述是小編給大家介紹的Vue 中使用 CSS Modules優(yōu)雅方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • vue-cli多頁面應(yīng)用實(shí)踐之實(shí)現(xiàn)組件預(yù)覽項(xiàng)目

    vue-cli多頁面應(yīng)用實(shí)踐之實(shí)現(xiàn)組件預(yù)覽項(xiàng)目

    在最近的項(xiàng)目中遇到了一個(gè)需求,找了相關(guān)資料后終于實(shí)現(xiàn),這篇文章主要給大家介紹了關(guān)于vue-cli多頁面應(yīng)用實(shí)踐之實(shí)現(xiàn)組件預(yù)覽項(xiàng)目的相關(guān)資料,需要的朋友可以參考下
    2022-05-05
  • vue在同一個(gè)頁面重復(fù)引用相同組件如何區(qū)分二者

    vue在同一個(gè)頁面重復(fù)引用相同組件如何區(qū)分二者

    這篇文章主要介紹了vue在同一個(gè)頁面重復(fù)引用相同組件如何區(qū)分二者,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • vue通過v-html指令渲染的富文本無法修改樣式的解決方案

    vue通過v-html指令渲染的富文本無法修改樣式的解決方案

    這篇文章主要介紹了vue通過v-html指令渲染的富文本無法修改樣式的解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • Vue-element-admin平臺(tái)側(cè)邊欄收縮控制問題

    Vue-element-admin平臺(tái)側(cè)邊欄收縮控制問題

    這篇文章主要介紹了Vue-element-admin平臺(tái)側(cè)邊欄收縮控制問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • Vue2.0腳手架搭建

    Vue2.0腳手架搭建

    這篇文章介紹了使用vue-cli搭建腳手架的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-03-03
  • Vue中router-link常用屬性使用案例講解

    Vue中router-link常用屬性使用案例講解

    這篇文章主要介紹了Vue中router-link常用屬性使用案例講解,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-01-01
  • vue基于better-scroll實(shí)現(xiàn)左右聯(lián)動(dòng)滑動(dòng)頁面

    vue基于better-scroll實(shí)現(xiàn)左右聯(lián)動(dòng)滑動(dòng)頁面

    這篇文章主要介紹了vue基于better-scroll實(shí)現(xiàn)左右聯(lián)動(dòng)滑動(dòng)頁面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • 詳細(xì)聊聊前端如何實(shí)現(xiàn)token無感刷新(refresh_token)

    詳細(xì)聊聊前端如何實(shí)現(xiàn)token無感刷新(refresh_token)

    實(shí)現(xiàn)token無感刷新對于前端來說是一項(xiàng)非常常用的技術(shù),其本質(zhì)是為了優(yōu)化用戶體驗(yàn),下面這篇文章主要給大家介紹了關(guān)于前端如何實(shí)現(xiàn)token無感刷新(refresh_token)的相關(guān)資料,需要的朋友可以參考下
    2022-11-11
  • VUEX如何使用

    VUEX如何使用

    Vuex?可以幫助我們管理共享狀態(tài),并附帶了更多的概念和框架,本文主要介紹了VUEX如何使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • vue form表單post請求結(jié)合Servlet實(shí)現(xiàn)文件上傳功能

    vue form表單post請求結(jié)合Servlet實(shí)現(xiàn)文件上傳功能

    這篇文章主要介紹了vue form表單post請求結(jié)合Servlet實(shí)現(xiàn)文件上傳功能,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01

最新評論