在vue中實(shí)現(xiàn)antd的動(dòng)態(tài)主題的代碼示例
在需求開(kāi)發(fā)階段,鑒于項(xiàng)目采用了antd作為基礎(chǔ)組件庫(kù),確保組件外觀與antd一致變得尤為重要,這包括顏色、字體大小及尺寸等樣式的統(tǒng)一。antd在其最新版本中,通過(guò)CSS變量(也稱為CSS自定義屬性)的形式,將所有樣式整合至<style>
標(biāo)簽內(nèi),極大地方便了開(kāi)發(fā)者通過(guò)var(--antd-xxx)
語(yǔ)法直接引用antd的樣式設(shè)置。
然而,截至當(dāng)前(版本4.2.3),antd-vue尚未實(shí)現(xiàn)這一便捷的CSS變量特性。但理解其背后的實(shí)現(xiàn)機(jī)制后,我們可以自行構(gòu)建這一功能。
實(shí)現(xiàn)思路簡(jiǎn)述:
- 獲取Antd樣式:利用
theme.userToken
接口(或類似機(jī)制,具體依據(jù)antd-vue或自定義主題的接口而定),提取antd的樣式定義。 - 注入樣式:將獲取到的樣式通過(guò)JavaScript動(dòng)態(tài)地添加到HTML文檔的
<style>
標(biāo)簽中,確保組件能夠訪問(wèn)到這些CSS變量。 - 動(dòng)態(tài)更新:為了支持樣式變量的動(dòng)態(tài)變更(如主題切換),需監(jiān)聽(tīng)
theme.userToken
或相關(guān)主題配置的變化,并實(shí)時(shí)更新DOM中的<style>
標(biāo)簽內(nèi)容,同時(shí)確保樣式更新的單一性和高效性,避免冗余或沖突。
通過(guò)以上步驟,即使antd-vue當(dāng)前版本未內(nèi)置CSS變量支持,我們也能通過(guò)自定義實(shí)現(xiàn)來(lái)達(dá)成與antd樣式的高度一致,提升開(kāi)發(fā)效率和組件的可用性。
具體實(shí)現(xiàn),下面代碼復(fù)制到到本地,并允許即可
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="https://registry.npmmirror.com/@babel/standalone/^7/files/babel.min.js"></script> <script src="https://registry.npmmirror.com/vue/3/files/dist/vue.global.js"></script> <script src="https://registry.npmmirror.com/dayjs/^1/files/dayjs.min.js"></script> <script src="https://registry.npmmirror.com/dayjs/1.11.11/files/plugin/customParseFormat.js"></script> <script src="https://registry.npmmirror.com/dayjs/1.11.11/files/plugin/advancedFormat.js"></script> <script src="https://registry.npmmirror.com/dayjs/1.11.11/files/plugin/weekday.js"></script> <script src="https://registry.npmmirror.com/dayjs/1.11.11/files/plugin/localeData.js"></script> <script src="https://registry.npmmirror.com/dayjs/1.11.11/files/plugin/weekOfYear.js"></script> <script src="https://registry.npmmirror.com/dayjs/1.11.11/files/plugin/weekYear.js"></script> <script src="https://registry.npmmirror.com/dayjs/1.11.11/files/plugin/quarterOfYear.js"></script> <script src="https://registry.npmmirror.com/ant-design-vue/4.2.3/files/dist/antd.min.js"></script> <style> .test { color: var(--antv-color-primary) } </style> </head> <body> <div id="root"></div> <script> Babel.registerPreset("env-plus", { presets: [ [Babel.availablePresets["env"]], [Babel.availablePresets['react'], { pragma: 'h', pragmaFrag: "Fragment", }] ], plugins: [], }); const toLine = (name) => { if (!name) return name return name.replace(/([A-Z])/g, '-$1').toLowerCase() } </script> <!-- 代碼開(kāi)始 --> <script type="text/babel" data-presets="env-plus"> const { createApp, ref, h, watch } = Vue const { Button, ConfigProvider, theme, Table } = antd const { useToken } = theme const styleDom = document.createElement('style') document.head.appendChild(styleDom) const setCssVar = (data) => { const list = Object.entries((data)).map(([key, val]) => { return `--antv-${toLine(key)}: ${val}` }).join(';') const css = `:root { ${list} }` styleDom.textContent = css } const App = { setup() { const { token } = useToken() watch(token, (newToken) => { setCssVar(newToken) }, { immediate: true }) const myTheme = ref({ colorPrimary: 'red', colorTextHeading: 'red', colorText: 'red', fontWeightStrong: 400 }) const handleTheme = () => { myTheme.value.colorPrimary = myTheme.value.colorPrimary === 'red' ? 'green' : 'red' } const dataSource = [ { key: '1', name: '胡彥斌', age: 32, address: '西湖區(qū)湖底公園1號(hào)', }, { key: '2', name: '胡彥祖', age: 42, address: '西湖區(qū)湖底公園1號(hào)', }, ]; const columns = [ { title: '姓名', dataIndex: 'name', key: 'name', }, { title: '年齡', dataIndex: 'age', key: 'age', }, { title: '住址', dataIndex: 'address', key: 'address', }, ] return () => { return <ConfigProvider theme={{ token: myTheme.value }} > <Button type="primary" onClick={handleTheme}>切換主題</Button> <div class='test'>使用--antv-colorPrimary變量</div> <Table columns={columns} dataSource={dataSource} /> </ConfigProvider> } }, } const app = createApp(App) app.mount('#root') </script> </body> </html>
到此這篇關(guān)于在vue中實(shí)現(xiàn)antd的動(dòng)態(tài)主題的代碼示例的文章就介紹到這了,更多相關(guān)vue antd動(dòng)態(tài)主題內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue electron應(yīng)用調(diào)exe程序的實(shí)現(xiàn)步驟
這篇文章主要介紹了vue electron應(yīng)用調(diào)exe程序的實(shí)現(xiàn)步驟,用Python寫了一個(gè)本地服務(wù)編譯成exe程序,在electron程序啟動(dòng)后,自動(dòng)執(zhí)行exe程序,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下2024-02-02vue實(shí)現(xiàn)密碼顯示與隱藏按鈕的自定義組件功能
本文通過(guò)兩種思路給大家介紹vue實(shí)現(xiàn)密碼顯示與隱藏按鈕的自定義組件功能,感興趣的朋友跟隨小編一起看看吧2019-04-04vue控制滾動(dòng)條滑到某個(gè)位置的方法實(shí)例
當(dāng)容器有滾動(dòng)條時(shí),有時(shí)需要將滾動(dòng)條滑到某個(gè)位置,下面這篇文章主要給大家介紹了關(guān)于vue控制滾動(dòng)條滑到某個(gè)位置的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12解決vue頁(yè)面刷新vuex中state數(shù)據(jù)丟失的問(wèn)題
這篇文章介紹了解決vue頁(yè)面刷新vuex中state數(shù)據(jù)丟失的問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01Vue計(jì)算屬性與偵聽(tīng)器和過(guò)濾器超詳細(xì)介紹
這篇文章主要介紹了Vue計(jì)算屬性與偵聽(tīng)器和過(guò)濾器,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10