src/
├── styles/
│ └── _variables.scss # 全局变量文件
├── page/
│ ├── home.scss # 页面样式
│ ├── device/
│ │ └── device.scss # 组件样式
│ ├── ai-chat/
│ │ ├── ai-chat.scss
│ │ ├── input/
│ │ │ └── input.scss
│ │ └── dialog/
│ │ └── dialog.scss
│ └── ...
_variables.scssdevice.scss、home.scss@import '../../styles/_variables'位置:src/styles/_variables.scss
$primary-color: #0769fb; // 主色调
$ai-chat-bg: #e0e8f4; // AI聊天背景色
$process-bg: #07fb40; // 进程背景色
$screenshot-bg: #fb0707e6; // 截图背景色
$white: #fff; // 白色
$black: #000; // 黑色
$spacing-base: 0; // 基础间距
$full-size: 100%; // 全尺寸
$flex-center: (
display: flex,
align-items: center,
justify-content: center
);
$reset: (
margin: 0,
padding: 0
);
// 在组件 SCSS 文件顶部导入
@import '../../styles/_variables';
.device-container {
background-color: $primary-color;
width: $full-size;
height: $full-size;
}
在 _variables.scss 中添加:
// 新颜色
$success-color: #28a745;
$danger-color: #dc3545;
// 新尺寸
$spacing-sm: 8px;
$spacing-md: 16px;
$spacing-lg: 24px;
// 1. 导入变量(文件顶部)
@import '../../styles/_variables';
// 2. 组件根类
.device-container {
// 样式
}
// 3. 嵌套子元素
.device-container {
.device-update {
// 样式
}
}
// 不要忘记导入变量
.device-container {
background-color: #0769fb; // ❌ 应该使用 $primary-color
}
.device-container {
width: $full-size;
.device-update {
padding: 16px;
.device-update-title {
font-size: 20px;
}
}
}
// ❌ 嵌套太深,难以维护
.container {
.wrapper {
.content {
.inner {
.title {
.text {
color: red;
}
}
}
}
}
}
.button {
background: $primary-color;
// 伪类
&:hover {
background: darken($primary-color, 10%);
}
// 修饰符
&.active {
background: #28a745;
}
// 伪元素
&::before {
content: '';
}
}
// 单行注释(推荐)
.device-container {
// 设备容器样式
width: 100%;
}
/*
* 多行注释
* 用于复杂说明
*/
@import '../../styles/_variables';
.container {
background-color: $primary-color;
color: $white;
}
.container {
background-color: #0769fb; // ❌ 硬编码
color: #fff;
}
// src/styles/_mixins.scss
@mixin flex-center {
display: flex;
align-items: center;
justify-content: center;
}
@mixin button-base {
padding: 12px 24px;
border-radius: 4px;
cursor: pointer;
transition: all 0.3s;
}
@import '../../styles/_variables';
@import '../../styles/_mixins';
.device-container {
@include flex-center;
width: $full-size;
}
.device-container {
width: 100%;
// 移动端
@media (max-width: 768px) {
padding: 10px;
.device-update {
flex-direction: column;
}
}
// 桌面端
@media (min-width: 1200px) {
max-width: 1200px;
margin: 0 auto;
}
}
每个组件有自己的 SCSS 文件:
// device/device.scss
.device-container {
// 只包含 device 组件的样式
}
// home.scss
.home-container {
// 只包含 home 组件的样式
}
// Flex 居中
@mixin flex-center {
display: flex;
align-items: center;
justify-content: center;
}
// 使用
.container {
@include flex-center;
}
@mixin clearfix {
&::after {
content: '';
display: table;
clear: both;
}
}
@mixin text-ellipsis {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
$breakpoints: (
'sm': 576px,
'md': 768px,
'lg': 992px,
'xl': 1200px
);
@mixin respond-to($breakpoint) {
$value: map-get($breakpoints, $breakpoint);
@if $value {
@media (min-width: $value) {
@content;
}
}
}
// 使用
.container {
width: 100%;
@include respond-to('md') {
width: 50%;
}
}
$primary: $primary-color;
// 变亮/变暗
lighten($primary, 20%)
darken($primary, 10%)
// 调整透明度
rgba($primary, 0.8)
// 混合颜色
mix($primary, white, 50%)
// 百分比
percentage(2/3) // 66.666667%
// 四舍五入
round(18.7px) // 19px
// 向上/向下取整
ceil(18.2px) // 19px
floor(18.9px) // 18px
A: 在 src/styles/_variables.scss 中添加:
// 新变量
$new-color: #ff0000;
$new-spacing: 20px;
然后在组件中导入使用:
@import '../../styles/_variables';
.container {
color: $new-color;
padding: $new-spacing;
}
A: 使用混入(Mixin):
// 在 _mixins.scss 中定义
@mixin card-style {
padding: 16px;
border-radius: 8px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}
// 使用
.card {
@include card-style;
}
A: 使用 SCSS 变量和类切换:
// _variables.scss
$themes: (
'light': (
'bg': #fff,
'text': #333
),
'dark': (
'bg': #1a1a1a,
'text': #fff
)
);
@function theme($theme-name, $key) {
$theme: map-get($themes, $theme-name);
@return map-get($theme, $key);
}
// 使用
.container {
background-color: theme('light', 'bg');
.dark-theme & {
background-color: theme('dark', 'bg');
}
}
检查清单:
.scssimport './component.scss'A: 使用 @debug 和 @warn:
$width: 100px;
@debug "Width is: #{$width}";
@function calculate($value) {
@if $value < 0 {
@warn "Value should be positive";
}
@return $value * 2;
}
主色调: #0769fb (蓝色)
AI聊天: #e0e8f4 (浅蓝灰)
进程: #07fb40 (绿色)
截图: #fb0707e6 (红色,带透明度)
home.scss - 首页布局device/device.scss - 设备管理ai-chat/ai-chat.scss - AI聊天process/process.scss - 进程管理screenshot/screenshot.scss - 截图功能public/alert-view/alert-view.scss - 弹窗组件_variables.scss最后更新:2025-01-30