vue3安裝配置sass的詳細(xì)步驟
前言:
對(duì)于前端開(kāi)發(fā)人員來(lái)說(shuō),css預(yù)處理的語(yǔ)言已經(jīng)是家常便飯了,如sass,less等等,那么在vue3中該如何去使用sass呢?
首先看個(gè)最基礎(chǔ)的頁(yè)面,木有任何的樣式,接下來(lái)將一步一步的添加樣式!
<template>
<div>
123456
</div>
</template>
1. 安裝sass
npm install sass
2. 新建style目錄,存放scss文件
項(xiàng)目src文件下,新建styles目錄,當(dāng)然位置自己隨意定,新建了這三個(gè)scss文件,下面我們對(duì)這三個(gè)文件進(jìn)行一一解析。

constant.scss:用于放置項(xiàng)目中的sass變量,比如主題顏色,大字體的字號(hào),小字體的字號(hào)等等,這里只是用于測(cè)試
$color-red: #ff0000; $large-size: 40px; $font-oblique: oblique;
index.scss:用于放置項(xiàng)目中自己封裝的一些常用的樣式,class類名,比如flex布局,定位,字體等等,這個(gè)只寫(xiě)了一個(gè)
@import "./constant.scss";
.l-size {
font-size: $large-size;
}variables.module.scss:用于scss變量的導(dǎo)出,大部分用于vue文件中js中使用
@import "./constant.scss";
:export {
fontOblique: $font-oblique;
}3. main.ts
將我們封裝的公共的css樣式類名導(dǎo)入進(jìn)main.ts文件中,這樣在所有的vue文件中,就可以隨意使用這些樣式了

嘗試一下~.~
<template>
<div class="l-size">
123456
</div>
</template>
4. vite.config.ts
主要用途是將我們的constant.scss中的scss常量加載到全局,這樣我們可以在style標(biāo)簽中,隨意使用這些scss常量
import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path';
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
resolve: {
// Vite路徑別名配置
alias: {
'@': path.resolve('./src')
}
},
/*主要看下面這段*/
css: {
preprocessorOptions: {
scss: {
additionalData: '@import "@/styles/constant.scss";'
}
}
}
})嘗試一下~.~
<template>
<div class="l-size content">
123456
</div>
</template>
<style lang="scss" scoped>
.content {
color: $color-red;
}
</style>
5. Test.vue
接下來(lái),我們將 variables.module.scss中的變量導(dǎo)入到當(dāng)前的vue文件中。
<script lang="ts" setup> import variables from "@/styles/variables.module.scss" console.log(variables) </script>

于是我們可以這么寫(xiě)
<template>
<div class="l-size content" :style="{fontStyle:variables.fontOblique}">
123456
</div>
</template>
<script setup lang="ts">
import variables from "../styles/variables.module.scss"
console.log(variables)
</script>
<style lang="scss" scoped>
.content {
color: $color-red;
}
</style>或者利用computed
<template>
<div class="l-size content" :style="getStyle">
123456
</div>
</template>
<script setup lang="ts">
import {computed} from "vue"
import variables from "../styles/variables.module.scss"
const getStyle = computed(() => ({fontStyle: variables.fontOblique}))
</script>
<style lang="scss" scoped>
.content {
color: $color-red;
}
</style>
如此這樣~.~!
總結(jié)
到此這篇關(guān)于vue3安裝配置sass的文章就介紹到這了,更多相關(guān)vue3安裝配置sass內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決axios發(fā)送post請(qǐng)求返回400狀態(tài)碼的問(wèn)題
今天小編就為大家分享一篇解決axios發(fā)送post請(qǐng)求返回400狀態(tài)碼的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
LRU算法在Vue內(nèi)置組件keep-alive中的使用
LRU算法全稱為least recently use 最近最少使用,核心思路是最近被訪問(wèn)的以后被訪問(wèn)的概率會(huì)變高,那么可以把之前沒(méi)被訪問(wèn)的進(jìn)行刪除,維持一個(gè)穩(wěn)定的最大容量值,從而不會(huì)導(dǎo)致內(nèi)存溢出。2021-05-05
vue實(shí)現(xiàn)圖片路徑轉(zhuǎn)二進(jìn)制文件流(binary)
這篇文章主要介紹了vue實(shí)現(xiàn)圖片路徑轉(zhuǎn)二進(jìn)制文件流(binary),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
前端實(shí)現(xiàn)簡(jiǎn)單的sse封裝方式(React hook Vue3)
這篇文章主要介紹了前端實(shí)現(xiàn)簡(jiǎn)單的sse封裝方式(React hook Vue3),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
Vue中splice()方法對(duì)數(shù)組進(jìn)行增刪改等操作的實(shí)現(xiàn)
vue中對(duì)數(shù)組的元素進(jìn)行刪除,以前一直以為這個(gè)方法是vue中特有的,后來(lái)百度之后才知道原來(lái)是js的一個(gè)寫(xiě)法,下面這篇文章主要給大家介紹了關(guān)于Vue中splice()方法對(duì)數(shù)組進(jìn)行增刪改等操作的實(shí)現(xiàn)方法,需要的朋友可以參考下2023-05-05
vue Element-ui表格實(shí)現(xiàn)樹(shù)形結(jié)構(gòu)表格
這篇文章主要為大家詳細(xì)介紹了vue Element-ui表格實(shí)現(xiàn)樹(shù)形結(jié)構(gòu)表格,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06
vue前端代碼如何通過(guò)maven打成jar包運(yùn)行
這篇文章主要介紹了vue前端代碼如何通過(guò)maven打成jar包運(yùn)行問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01

