vue3 集成 Sass

创建全局默认样式
在src 文件夹下 创建 styles 并创建 index.scss 引入到项目

import '@/styles/index.scss'

配置scss 全局变量 @import “./src/styles/variable.scss”; 冒号不能少不然会报错

export default defineConfig({
  plugins: [
    vue(),
    createSvgIconsPlugin({
      // 指定需要缓存的图标文件夹
      iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
      // 指定symbolId格式
      symbolId: 'icon-[dir]-[name]',
    })
  ],
  // 配置 @ 导入
  resolve: {
    alias: {
      "@": path.resolve("./src")
    }
  },
  // 配置scss 全局配置变量
  css: {
    preprocessorOptions: {
      scss: {
        javascriptEnabled: true,
        additionalData: '@import "./src/styles/variable.scss";',
      }
    }
  }
})

清除全局样式 reset.scss

/**
 * ENGINE
 * v0.2 | 20150615
 * License: none (public domain)
 */

 *,
 *:after,
 *:before {
     box-sizing: border-box;
 
     outline: none;
 }
 
 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 {
     font: inherit;
     font-size: 100%;
 
     margin: 0;
     padding: 0;
 
     vertical-align: baseline;
 
     border: 0;
 }
 
 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;
     &:before,
     &:after {
         content: '';
         content: none;
     }
 }
 
 sub,
 sup {
     font-size: 75%;
     line-height: 0;
 
     position: relative;
 
     vertical-align: baseline;
 }
 sup {
     top: -.5em;
 }
 sub {
     bottom: -.25em;
 }
 
 table {
     border-spacing: 0;
     border-collapse: collapse;
 }
 
 input,
 textarea,
 button {
     font-family: inhert;
     font-size: inherit;
 
     color: inherit;
 }
 
 select {
     text-indent: .01px;
     text-overflow: '';
 
     border: 0;
     border-radius: 0;
 
     -webkit-appearance: none;
        -moz-appearance: none;
 }
 select::-ms-expand {
     display: none;
 }
 
 code,
 pre {
     font-family: monospace, monospace;
     font-size: 1em;
 }
Vue 3 中使用 Vite 集成 Sass 预处理器的步骤如下: 1. 首先,确保你已经安装了最新版本的 Vue CLI。你可以使用以下命令进行安装: ``` npm install -g @vue/cli ``` 2. 创建一个新的 Vue 3 项目。使用以下命令: ``` vue create my-project ``` 3. 在创建项目时,选择 "Manually select features",然后按下空格键选择 "Router" 和 "Vuex",最后按下 Enter 键。 4. 进入到项目目录中: ``` cd my-project ``` 5. 安装 Vite 插件和相关依赖项: ``` npm install vite @vitejs/plugin-vue @vitejs/plugin-vue-jsx sass -D ``` 6. 创建一个 `vite.config.js` 文件,并添加以下内容: ```javascript import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import vueJsx from '@vitejs/plugin-vue-jsx' import { vitePlugin as vitePluginSass } from 'vite-plugin-sass' // https://vitejs.dev/config/ export default defineConfig({ plugins: [ vue(), vueJsx(), vitePluginSass() ] }) ``` 7. 在 `package.json` 文件中,将 "scripts" 部分中的 "serve" 和 "build" 命令修改为: ```json "scripts": { "serve": "vite", "build": "vite build" } ``` 8. 创建一个 `src/styles/main.scss` 文件,并在其中编写你的 Sass 样式。 9. 在 `src/main.js` 文件中引入 `src/styles/main.scss`: ```javascript import './styles/main.scss' ``` 10. 运行项目: ``` npm run serve ``` 现在,你的 Vue 3 项目已经集成Sass 预处理器。你可以在组件中使用 Sass 的语法编写样式。 希望这些步骤对你有帮助!如果你有任何其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值