vue3中安装并使用CSS预处理器Sass的方法介绍

Sass(Syntactically Awesome Style Sheets)是一种CSS预处理器,它扩展了CSS的功能,提供了更高级的语法和特性,例如变量、嵌套、混合、继承和颜色功能等,这些特性可以帮助开发者更高效地管理和维护样式表。Sass语法是基于缩进(缩进花括号)的,它与传统的CSS语法类似,但添加了一些额外的规则和语法结构。Sass代码需要编译成CSS代码才能在浏览器中呈现。

1. 安装sass

以前用vue-cli的时候,还要安装sass-loader、node-sass什么的,安装的时候还会遇到各种问题,但是vite其实安装sass就可以了,很简单

npm install sass -D

2. 编写全局css变量/全局mixin

// 全局变量 / globalVar.scss
$font-size-normal: 32px;

$bg-color: #1989fa;

// 全局mixin / globalMixin.scss
@mixin box-shadow($bulr: 20px, $color: #1989fa7a) {
    -webkit-box-shadow: 0px 0px $bulr $color;
    -moz-box-shadow: 0px 0px $bulr $color;
    box-shadow: 0px 0px $bulr $color;
}

3.vite引入并使用

//全局引入
css: {
    preprocessorOptions: {
      scss: {
        /**如果引入多个文件,可以使用
       * '@import "@/assets/scss/globalVariable1.scss";
       * @import"@/assets/scss/globalVariable2.scss";'
      **/
        additionalData: '@import "@/style/globalVar.scss";',
      }
    }
  },

4.按需引入并使用

<style scoped lang="scss">
@import "@/style/globalMixin.scss";
.test{
  width: 650px;
  height: 60px;
  font-size: $font-size-normal;
  background-color: $bg-color;
  @include box-shadow;
}

相关推荐

  1. vue3安装使用CSS处理器Sass方法介绍

    2023-12-27 07:44:03       63 阅读
  2. CSS处理器---Sass/Scss

    2023-12-27 07:44:03       75 阅读
  3. 前端-CSS处理器Sass

    2023-12-27 07:44:03       56 阅读
  4. Sass详解:CSS处理器强大之处

    2023-12-27 07:44:03       34 阅读
  5. SCSS基本使用:解锁CSS处理器高效与优雅

    2023-12-27 07:44:03       39 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-27 07:44:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-27 07:44:03       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-27 07:44:03       87 阅读
  4. Python语言-面向对象

    2023-12-27 07:44:03       97 阅读

热门阅读

  1. Redis Stream消息队列之基本语法与使用方式

    2023-12-27 07:44:03       40 阅读
  2. [oracle数据库]dblink的使用

    2023-12-27 07:44:03       63 阅读
  3. 如何将自建的ElasticSearch注册成一个服务

    2023-12-27 07:44:03       59 阅读
  4. codeforces 1676F

    2023-12-27 07:44:03       65 阅读
  5. latexshop 使用bug:xxx has a comma at the end

    2023-12-27 07:44:03       56 阅读
  6. c++ qt QtWidgetsApplication 项目 使用外部ui

    2023-12-27 07:44:03       61 阅读
  7. GO基础进阶篇 (八)、runtime包

    2023-12-27 07:44:03       67 阅读
  8. k8s解决 搭建集群的时候notReady问题

    2023-12-27 07:44:03       62 阅读
  9. 【Go语言入门:Go程序的流程控制语句】

    2023-12-27 07:44:03       50 阅读
  10. client-go使用方法

    2023-12-27 07:44:03       61 阅读
  11. Unity编辑器紫色

    2023-12-27 07:44:03       58 阅读
  12. mysql如何分析sql是否成功使用索引

    2023-12-27 07:44:03       68 阅读