Vue3集成sass的詳細(xì)過程
安裝依賴
pnpm add -D sass-embedded
配置全局變量
- 新建文件
src/styles/variables.scss
- 配置Vite 修改
vite.config.ts
variables.scss
$base-color: blue
vite.config.ts
// https://vite.dev/config/ export default defineConfig({ plugins: [ vue(), ], resolve: { alias: { '@': fileURLToPath(new URL('./src', import.meta.url)) //相對(duì)路徑別名配置,@代替src,用于打包時(shí)路徑轉(zhuǎn)換 }, }, css: { preprocessorOptions: { // 引入公共scss變量 scss: { // 引入 varibles.scss 這樣就可以在全局中使用 varibles.scss中預(yù)定義的變量了 // 給導(dǎo)入的路徑最后加上 ; // as 后面是定義的環(huán)境變量 // 如果寫as * ,可以直接使用變量名,如:變量名 // 如果不寫as *, 默認(rèn)文件名即:variables, 使用variables.變量名 // 如果是as vars,則使用vars.變量名 additionalData: '@use "@/assets/styles/variables" as *;', }, }, }, })
引入全局樣式
- 創(chuàng)建文件
src/assets/styles/reset.scss
,src/assets/styles/index.scss
- 引入全局樣式
main.ts
reset.scss
// 重置樣式 https://www.npmjs.com/package/reset.css?activeTab=code html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video { margin: 0; padding: 0; border: 0; font-size: 100%; font: inherit; vertical-align: baseline; } /* HTML5 display-role reset for older browsers */ article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section { display: block; } body { line-height: 1; } ol, ul { list-style: none; } blockquote, q { quotes: none; } blockquote:before, blockquote:after, q:before, q:after { content: ''; content: none; } table { border-collapse: collapse; border-spacing: 0; }
index.scss
// 引入重置樣式 @use 'reset';
main.ts
... const app = createApp(App) ... // 引入全局樣式 import '@/assets/styles/index.scss' app.mount('#app')
使用測(cè)試
<template> <div class="home"> <h2>Home</h2> </div> </template> <script setup lang="ts"> </script> <style scoped lang="scss"> .home { h2 { color: $base-color; } } </style>
到此這篇關(guān)于Vue3集成sass的文章就介紹到這了,更多相關(guān)Vue集成sass內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue3文件下載方法實(shí)現(xiàn)的簡(jiǎn)單代碼
在Web開發(fā)中,文件下載可通過多種方式實(shí)現(xiàn),下面這篇文章主要介紹了Vue3文件下載方法實(shí)現(xiàn)的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-10-10vue定時(shí)器清除不掉,導(dǎo)致功能頻繁執(zhí)行問題
這篇文章主要介紹了vue定時(shí)器清除不掉,導(dǎo)致功能頻繁執(zhí)行問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06關(guān)于vue?src路徑動(dòng)態(tài)拼接的小知識(shí)
這篇文章主要介紹了vue?src路徑動(dòng)態(tài)拼接的小知識(shí),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2022-04-04vue中$event使用之獲取當(dāng)前元素及相關(guān)元素
這篇文章主要介紹了vue中$event使用之獲取當(dāng)前元素及相關(guān)元素,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10Vue使用axios進(jìn)行g(shù)et請(qǐng)求拼接參數(shù)的2種方式詳解
axios中post請(qǐng)求都是要求攜帶參數(shù)進(jìn)行請(qǐng)求,這篇文章主要給大家介紹了關(guān)于Vue使用axios進(jìn)行g(shù)et請(qǐng)求拼接參數(shù)的2種方式,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01