Vue3 使用 Sass/SCSS 预处理器指南

Marimo_z
2025-04-23 / 7 评论 / 436 阅读 / 正在检测是否收录...

1. 安装依赖

npm install -D sass-embedded

该包提供了最新版本的 Dart Sass 嵌入式编译器,性能优于传统 Ruby Sass 和部分 Node 版本。


2. 基础配置

Vue 单文件组件使用:

<style lang="scss">
/* 直接编写 SCSS 代码 */
</style>

全局样式配置(vue.config.js):

module.exports = {
  css: {
    loaderOptions: {
      scss: {
        // 全局注入变量/混合宏(适用于所有组件)
        additionalData: `@import "@/styles/global-vars.scss";`
      }
    }
  }
}

3. 基础使用示例

组件内使用:

<template>
  <div class="demo-box">
    <button class="styled-button">Click me</button>
  </div>
</template>

<style lang="scss" scoped>
// 局部变量
$primary-color: #42b983;
$border-radius: 8px;

.demo-box {
  padding: 2rem;
  background: lighten($primary-color, 45%);

  .styled-button {
    padding: 12px 24px;
    background: $primary-color;
    border-radius: $border-radius;
    
    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 4px 15px rgba($primary-color, 0.3);
    }
  }
}
</style>

4. 核心语法详解

1. 变量定义

$font-stack: Helvetica, sans-serif;
$primary-color: #333;
$spacing-unit: 1rem;

body {
  font: 100% $font-stack;
  color: $primary-color;
  margin: $spacing-unit * 2;
}

2. 嵌套规则

.navbar {
  background: #f8f9fa;
  
  ul {
    margin: 0;
    padding: 0;
    list-style: none;

    li {
      display: inline-block;
      
      a {
        padding: 0.5rem;
        &:hover {
          text-decoration: underline;
        }
      }
    }
  }
}

3. 混合宏 (Mixin)

// 定义
@mixin center-flex($direction: row) {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: $direction;
}

// 使用
.container {
  @include center-flex(column);
  min-height: 100vh;
}

4. 继承 (Extend)

%button-base {
  padding: 12px 24px;
  border-radius: 4px;
  font-size: 1rem;
}

.primary-button {
  @extend %button-base;
  background: #42b983;
  color: white;
}

.outline-button {
  @extend %button-base;
  border: 2px solid #42b983;
}

5. 模块化导入

// variables.scss
$breakpoint-md: 768px;

// mixins.scss
@mixin respond-to($breakpoint) {
  @media (min-width: $breakpoint) {
    @content;
  }
}

// main.scss
@use 'variables' as vars;
@use 'mixins';

.header {
  padding: 2rem;
  
  @include mixins.respond-to(vars.$breakpoint-md) {
    padding: 3rem;
  }
}

6. 运算与函数

$base-size: 16px;

.container {
  font-size: $base-size;
  width: 100% - 20%; // 80%
  height: calc(100vh - #{$base-size * 2});
  background: darken(#42b983, 10%);
}

7. 条件语句

@mixin theme-colors($theme) {
  @if $theme == 'dark' {
    background: #333;
    color: white;
  } @else if $theme == 'light' {
    background: #fff;
    color: #333;
  } @else {
    background: #f0f0f0;
  }
}

.theme-box {
  @include theme-colors('dark');
}

5. 最佳实践

  1. 文件组织:

    src/
      styles/
        ├── abstracts/
        │   ├── _variables.scss
        │   ├── _mixins.scss
        ├── components/
        │   ├── _buttons.scss
        ├── main.scss
  2. 变量命名:

    // 颜色
    $color-primary: #42b983;
    $color-danger: #ff4757;
    
    // 间距
    $spacing-sm: 0.5rem;
    $spacing-md: 1rem;
    
    // 响应式断点
    $breakpoint-md: 768px;
  3. 响应式处理:

    @mixin respond-to($breakpoint) {
      @media (min-width: $breakpoint) {
        @content;
      }
    }
    
    .card {
      width: 100%;
      
      @include respond-to($breakpoint-md) {
        width: 50%;
      }
    }

6. 常见问题排查

1. 样式未生效:

  • 检查 <style> 标签是否包含 lang="scss"
  • 确认变量/混合宏的作用域(全局注入需要配置)
  • 检查选择器嵌套是否正确

2. 安装后报错:

# 确保 package.json 包含
"devDependencies": {
  "sass-embedded": "^1.0.0"
}
  • 清除 node_modules 后重新安装
  • 检查与其他 sass 包的版本冲突

3. 全局变量无法使用:

// vue.config.js 正确配置
additionalData: `
  @import "@/styles/variables.scss";
  @import "@/styles/mixins.scss";
`
1

评论 (7)

取消
  1. 头像
    khochwtmkv
    Windows 10 · Google Chrome

    2025年10月新盘 做第一批吃螃蟹的人coinsrore.com
    新车新盘 嘎嘎稳 嘎嘎靠谱coinsrore.com
    新车首发,新的一年,只带想赚米的人coinsrore.com
    新盘 上车集合 留下 我要发发 立马进裙coinsrore.com
    做了几十年的项目 我总结了最好的一个盘(纯干货)coinsrore.com
    新车上路,只带前10个人coinsrore.com
    新盘首开 新盘首开 征召客户!!!coinsrore.com
    新项目准备上线,寻找志同道合 的合作伙伴coinsrore.com
    新车即将上线 真正的项目,期待你的参与coinsrore.com
    新盘新项目,不再等待,现在就是最佳上车机会!coinsrore.com
    新盘新盘 这个月刚上新盘 新车第一个吃螃蟹!coinsrore.com

    回复
  2. 头像

    新盛客服电话是多少?(?183-8890-9465—《?薇-STS5099】【
    新盛开户专线联系方式?(?183-8890--9465—《?薇-STS5099】【?扣6011643??】
    新盛客服开户电话全攻略,让娱乐更顺畅!(?183-8890--9465—《?薇-STS5099】客服开户流程,华纳新盛客服开户流程图(?183-8890--9465—《?薇-STS5099】

    回复
  3. 头像

    果博东方客服开户联系方式【182-8836-2750—】?薇- cxs20250806】
    果博东方公司客服电话联系方式【182-8836-2750—】?薇- cxs20250806】
    果博东方开户流程【182-8836-2750—】?薇- cxs20250806】
    果博东方客服怎么联系【182-8836-2750—】?薇- cxs20250806】

    回复
  4. 头像

    果博东方客服开户联系方式【182-8836-2750—】?薇- cxs20250806】
    果博东方公司客服电话联系方式【182-8836-2750—】?薇- cxs20250806】
    果博东方开户流程【182-8836-2750—】?薇- cxs20250806】
    果博东方客服怎么联系【182-8836-2750—】?薇- cxs20250806】

    回复
  5. 头像

    东方明珠客服开户联系方式【182-8836-2750—】?μ- cxs20250806
    东方明珠客服电话联系方式【182-8836-2750—】?- cxs20250806】
    东方明珠开户流程【182-8836-2750—】?薇- cxs20250806】
    东方明珠客服怎么联系【182-8836-2750—】?薇- cxs20250806】

    回复
  6. 头像

    华纳圣淘沙开户步骤详解(183-8890-9465—?薇-STS5099【6011643】

    华纳圣淘沙公司开户流程全解析(183-8890-9465—?薇-STS5099【6011643】
    华纳圣淘沙公司账户注册指南(183-8890-9465—?薇-STS5099【6011643】
    新手如何开通华纳圣淘沙公司账户(183-8890-9465—?薇-STS5099【6011643】
    华纳圣淘沙企业开户标准流程(183-8890-9465—?薇-STS5099【6011643】
    华纳圣淘沙公司开户:从零到一(183-8890-9465—?薇-STS5099【6011643】
    官方指南:华纳圣淘沙公司开户流程(183-8890-9465—?薇-STS5099【6011643】
    华纳圣淘沙公司开户流程说明书(183-8890-9465—?薇-STS5099【6011643】

    回复
  7. 头像

    《华纳圣淘沙公司开户流程全解析》→ 官方顾问一对一指导??? 安全联系:183第三段8890第四段9465
    《华纳圣淘沙开户步骤详解》→ 」专属通道快速办理??? 安全联系:183第三段8890第四段9465
    《华纳圣淘沙账户注册指南》→ 扫码获取完整资料清单?「微?? 安全联系:183第三段8890第四段9465
    《新手开通华纳圣淘沙公司账户指南》→ 限时免费咨询开放??? 安全联系:183第三段8890第四段9465
    《华纳圣淘沙企业开户标准流程》→ 资深顾问实时解答疑问??? 安全联系:183第三段8890第四段9465
    《华纳圣淘沙开户步骤全景图》→ 点击获取极速开户方案??? 安全联系:183第三段8890第四段9465
    《华纳圣淘沙账户创建全流程手册》→ 预约顾问免排队服务?9?? 安全联系:183第三段8890第四段9465 《从零开通华纳圣淘沙公司账户》→ 添加客服领取开户工具包?? 安全联系:183第三段8890第四段9465
    《官方授权:华纳圣淘沙开户流程》→ 认证顾问全程代办?」?? 安全联系:183第三段8890第四段9465
    《华纳圣淘沙开户说明书》→立即联系获取电子版文件??? 安全联系:183第三段8890第四段9465

    回复