跳到内容
SCRM DOC

Less、UnoCSS 样式库使用说明

前端样式创建于2024-02-15更新于 2025-03-22

介绍

主要介绍如何在项目中使用和规划样式文件。

默认使用 Less 作为预处理语言,配合 UnoCSS 原子化 CSS 框架。

项目中使用的通用样式,都存放于 src/design/ 下面。

.
├── ant # ant design 一些样式覆盖
├── color.less # 颜色
├── index.less # 入口
├── public.less # 公共类
├── theme.less # 主题相关
├── config.less  # 每个组件都会自动引入样式
├── transition # 动画相关
└── var # 变量

全局注入

config.less 这个文件会被全局注入到所有文件,所以在页面内可以直接使用变量而不需要手动引入。

<style lang="less" scoped>
  /* 这里已经隐式注入了 config.less */
</style>

UnoCSS

项目当前使用 UnoCSS 作为原子化 CSS 框架(替代了原 WindiCSS)。

配置文件

配置位于项目根目录 uno.config.ts:

import { defineConfig, presetUno } from "unocss";
import presetIcons from "@unocss/preset-icons";
import { FileSystemIconLoader } from "@iconify/utils/lib/loader/node-loaders";
import fs from "fs";

// 本地 SVG 图标存放目录
const iconsDir = "./src/assets/icons";

// 读取本地 SVG 目录,自动生成 safelist
const generateSafeList = () => {
  try {
    return fs
      .readdirSync(iconsDir)
      .filter((file) => file.endsWith(".svg"))
      .map((file) => `i-svg:${file.replace(".svg", "")}`);
  } catch (error) {
    console.error("无法读取图标目录:", error);
    return [];
  }
};

export default defineConfig({
  presets: [
    presetUno(),
    presetIcons({
      extraProperties: {
        width: "1em",
        height: "1em",
        display: "inline-block",
      },
      collections: {
        svg: FileSystemIconLoader(iconsDir, (svg) => {
          return svg.includes('fill="')
            ? svg
            : svg.replace(/^<svg /, '<svg fill="currentColor" ');
        }),
      },
    }),
  ],
  safelist: generateSafeList(),
});

使用方式

<!-- 布局类 -->
<div class="flex items-center justify-center h-screen"></div>

<!-- 间距类 -->
<div class="p-4 m-2"></div>

<!-- 文本类 -->
<div class="text-lg font-bold text-center"></div>

<!-- 颜色类 -->
<div class="bg-blue-500 text-white"></div>

<!-- 本地 SVG 图标 (i-svg:图标名) -->
<span class="i-svg:logo text-4xl"></span>

<!-- Iconify 图标 -->
<span class="i-mdi:home text-xl"></span>

常用工具类

类别示例说明
布局flex, grid, block, inline-block显示方式
间距p-4, m-2, px-2, py-1padding/margin
尺寸w-full, h-screen, text-4xl宽高/字号
颜色bg-blue-500, text-white背景色/文字色
弹性flex-1, items-center, justify-betweenFlex 相关

为什么使用 Less

主要是因为 Ant Design 默认使用 less 作为样式语言,使用 Less 可以跟其保持一致。

开启 scoped

没有加 scoped 属性,默认会编译成全局样式,可能会造成全局污染。

<style></style>

<style scoped></style>

使用 scoped 后,父组件的样式将不会渗透到子组件中。不过一个子组件的根节点会同时受其父组件的 scoped CSS 和子组件的 scoped CSS 的影响。

深度选择器

有时我们可能想明确地制定一个针对子组件的规则。

如果你希望 scoped 样式中的一个选择器能够作用得"更深",例如影响子组件,你可以使用 >>> 操作符。有些像 Sass 之类的预处理器无法正确解析 >>>。这种情况下你可以使用 /deep/ 或 ::v-deep 操作符取而代之——两者都是 >>> 的别名,同样可以正常工作。

<style scoped>
  /* deep selectors */
  ::v-deep(.foo) { }
  /* shorthand */
  :deep(.foo) { }

  /* targeting slot content */
  ::v-slotted(.foo) { }
  /* shorthand */
  :slotted(.foo) { }

  /* one-off global rule */
  ::v-global(.foo) { }
  /* shorthand */
  :global(.foo) { }
</style>

CSS Modules

针对样式覆盖问题,还有一种方案是使用 CSS Modules 模块化方案。使用方式如下:

<template>
  <span :class="$style.span1">hello</span>
</template>

<script>
  import { useCSSModule } from 'vue';

  export default {
    setup(props, context) {
      const $style = useCSSModule();
      const moduleAStyle = useCSSModule('moduleA');
      return {
        $style,
        moduleAStyle,
      };
    },
  };
</script>

<style lang="less" module>
  .span1 {
    color: green;
    font-size: 30px;
  }
</style>

<style lang="less" module="moduleA">
  .span1 {
    color: green;
    font-size: 30px;
  }
</style>

重复引用问题

加上reference可以解决页面内重复引用导致实际生成的 style 样式表重复的问题。

这步已经全局引入了。所以可以不写,直接使用变量。

<style lang="less" scoped>
  /* 该行代码已全局引用。可以不用单独引入 */
  @import (reference) '../../design/config.less';
</style>
页面信息›
已优化是

本页目录