Vue3新特性之在Composition API中使用CSS Modules
在 Vue 3 Composition API 最近的一次 beta 升級中,無論是 Vue 3 本 3 庫 vue-next,還是面向 Vue 2 過渡用的 @vue/composition-api 庫中,都同步更新了一個 useCSSModule 函數(shù) -- 用來在使用了 Composition API 的 Vue 實例中,支持 CSS Modules 語法。
首先來看看什么是 CSS Modules:
CSS Modules
CSS Modules 是一種 CSS 的模塊化和組合系統(tǒng)。vue-loader 集成 CSS Modules,可以作為模擬 scoped CSS 的替代方案。
啟用 CSS Modules
如果是使用 vue cli 創(chuàng)建的項目,應(yīng)該已經(jīng)默認(rèn)開啟了這一特性;如果項目中需要手動開啟,按如下設(shè)置:
// webpack.config.js
{
module: {
rules: [
// ... 其它規(guī)則省略
{
test: /\.css$/,
use: [
'vue-style-loader',
{
loader: 'css-loader',
options: {
// 開啟 CSS Modules
modules: true,
// 自定義生成的類名
localIdentName: '[local]_[hash:base64:8]'
}
}
]
}
]
}
}
或者與其它預(yù)處理器一起使用:
// webpack.config.js -> module.rules
{
test: /\.scss$/,
use: [
'vue-style-loader',
{
loader: 'css-loader',
options: { modules: true }
},
'sass-loader'
]
}
然后在組件中的 <style> 上添加 module 特性:
<style module>
.red {
color: red;
}
.bold {
font-weight: bold;
}
</style>
在組件中訪問 CSS Modules
在 <style> 上添加 module 后,一個叫做 $style 的計算屬性就會被自動注入組件。
<template> <div> <p :class="$style.red"> hello red! </p> </div> </template>
因為這是一個計算屬性,所以也支持 :class 的對象/數(shù)組語法:
<template>
<div>
<p :class="{ [$style.red]: isRed }">
Am I red?
</p>
<p :class="[$style.red, $style.bold]">
Red and bold
</p>
</div>
</template>
也可以通過 JavaScript 訪問:
<script>
export default {
created () {
console.log(this.$style.red)
}
}
</script>
Vue 2.x 傳統(tǒng)用法
在 Options API 組件中:
<template>
<span :class="$style.span1">hello 111 - {{ text }}</span>
</template>
<script>
export default {
props: {
text: {
type: String,
default: ''
}
}
};
</script>
<style lang="scss" module>
.span1 {
color: red;
font-size: 50px;
}
</style>
對于 JSX 組件,由于其沒辦法用 scoped style,所以 CSS Modules 是個很好的選擇:
<script>
export default {
props: {
text: {
type: String,
default: ''
}
},
render(h) {
return <span class={this.$style.span1}>hello 222 - {this.text}</span>;
}
};
</script>
<style lang="scss" module>
.span1 {
color: blue;
font-size: 40px;
}
</style>
Vue 3.x 中的 useCSSModule
引入 useCSSModule 函數(shù)后,在 Composition API 組件中使用 CSS Modules 就有了標(biāo)準(zhǔn)方案:
<template>
<span :class="$style.span1">hello 333 - {{ text }}</span>
</template>
<script>
import { useCSSModule } from '@vue/composition-api';
export default {
props: {
text: {
type: String,
default: ''
}
},
setup(props, context) {
const $style = useCSSModule();
return {
$style
};
}
};
</script>
<style lang="scss" module>
.span1 {
color: green;
font-size: 30px;
}
</style>
其源碼實現(xiàn)也是非常之簡單,基本就是取出 this.$style 而已:
export const useCSSModule = (name = '$style'): Record<string, string> => {
const instance = getCurrentInstance()
if (!instance) {
__DEV__ && warn(`useCSSModule must be called inside setup()`)
return EMPTY_OBJ
}
const mod = (instance as any)[name]
if (!mod) {
__DEV__ &&
warn(`Current instance does not have CSS module named "${name}".`)
return EMPTY_OBJ
}
return mod as Record<string, string>
}
自定義 CSS Modules 注入名稱
通過觀察 useCSSModule 的源碼發(fā)現(xiàn),CSS Modules 的 name 好像可以不只是 $style;確實,在 .vue 文件中可以定義不止一個 <style module>,這可以通過設(shè)置 module 特性的值實現(xiàn):
<style module="a"> /* 注入標(biāo)識符 a */ </style> <style module="b"> /* 注入標(biāo)識符 b */ </style>
到此這篇關(guān)于Vue3新特性之在Composition API中使用CSS Modules的文章就介紹到這了,更多相關(guān)Vue3新特性之在Composition API中使用CSS Modules內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于Vue在ie10下空白頁的debug小結(jié)
這篇文章主要給大家介紹了關(guān)于Vue在ie10下空白頁的debug相關(guān)資料,這是最近在工作中遇到的一個問題,通過查找相關(guān)的資料終于解決了,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-05-05
使用vue進(jìn)行Lodop打印的一些常用方法小結(jié)
這篇文章主要給大家介紹了關(guān)于使用vue進(jìn)行Lodop打印的一些常用方法,需要進(jìn)行打印功能,Lodop就是實現(xiàn)需求的插件,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07
詳解在Vue.js編寫更好的v-for循環(huán)的6種技巧
這篇文章主要介紹了詳解在Vue.js編寫更好的v-for循環(huán)的6種技巧,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
vue 監(jiān)聽 Treeselect 選擇項的改變操作
這篇文章主要介紹了vue 監(jiān)聽 Treeselect 選擇項的改變操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08

