在 uniapp 里使用 unocss,vue3 + vite 项目

一、创建项目方式:vue-cli

二、基本的安装

node 复制代码
npm install -D unocss

三、vite.config.js

js 复制代码
import { defineConfig } from 'vite'
import uni from '@dcloudio/vite-plugin-uni'
import UnoCSS from 'unocss/vite'
// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    uni(),
    UnoCSS()
  ],
  server: {
      port: 6321,
      host: '0.0.0.0',
	  }
})

四、main.js

js 复制代码
import {
	createSSRApp
} from "vue";
import App from "./App.vue";

import 'virtual:uno.css'
export function createApp() {
	const app = createSSRApp(App);
	return {
		app,
	};
}

五、unocss.config.js

unocss-preset-weapp 是一个专为 微信小程序 和 UniApp 设计的 UnoCSS 预设,它内置了 rpx 单位支持,并可以自动处理 rem 转换问题10。

安装:

node 复制代码
npm i unocss-preset-weapp -D
js 复制代码
// unocss.config.js

import presetWeapp from "unocss-preset-weapp";

import {
  transformerClass,
  transformerAttributify,
} from "unocss-preset-weapp/transformer";
import { defineConfig, presetAttributify } from "unocss";
const directionMap = {
  t: "top",
  b: "bottom",
  r: "right",
  l: "left",
  x: ["left", "right"], // 水平方向
  y: ["top", "bottom"], // 垂直方向
};

export default {
  presets: [
    presetAttributify(),
    presetWeapp({
      whRpx: true, // 启用 rpx 单位(默认 true)
      platform: "uniapp", // 或 'uniapp'
      designWidth: 750,
    }),
  ],
  transformers: [transformerAttributify(), transformerClass()],
  shortcuts: [],
  rules: [
    // 基础间距
    [/^m-(\d+)$/, ([, d]) => ({ margin: `${d}rpx` })],
    [/^p-(\d+)$/, ([, d]) => ({ padding: `${d}rpx` })],

    // 方向性间距(修正版)
    [
      /^m([tbrlxy])-(\d+)$/,
      ([, dir, d]) => {
        const directions = Array.isArray(directionMap[dir])
          ? directionMap[dir]
          : [directionMap[dir]];

        return Object.fromEntries(
          directions.map((k) => [`margin-${k}`, `${d}rpx`])
        );
      },
    ],

    [
      /^p([tbrlxy])-(\d+)$/,
      ([, dir, d]) => {
        const directions = Array.isArray(directionMap[dir])
          ? directionMap[dir]
          : [directionMap[dir]];

        return Object.fromEntries(
          directions.map((k) => [`padding-${k}`, `${d}rpx`])
        );
      },
    ],

    // 负值支持
    [
      /^-m([tbrlxy])?-(\d+)$/,
      ([, dir, d]) => {
        if (!dir) return { margin: `-${d}rpx` };

        const directions = Array.isArray(directionMap[dir])
          ? directionMap[dir]
          : [directionMap[dir]];

        return Object.fromEntries(
          directions.map((k) => [`margin-${k}`, `-${d}rpx`])
        );
      },
    ],
    [
      "center",
      {
        position: "absolute",
        top: "50%",
        left: "50%",
        transform: `translate(-50%, -50%)`,
      },
    ],
    [
      "shadow1",
      {
        "box-shadow": "0px 0px 12px rgba(0, 0, 0, .12)",
      },
    ],
    [
      /^border-#([0-9a-fA-F]+)$/,
      ([_, color]) => ({
        border: `1px solid #${color}`,
      }),
    ],
    [
      "trans",
      {
        transition: ".3s",
      },
    ],
  ],
};

为什么这里要写这么一大段自定义规则,因为这个插件有bug,目前已经反馈了,希望以后可以更新修复,贴上 github issue 截图

相关推荐
程序员大阳35 分钟前
副队长 Vue3 教程(21)--【练手】封装通用弹窗组件
vue·组件·封装·通用·弹窗组件·父子传参
用户69371750013843 小时前
大模型蒸馏到底在蒸什么?一篇文章搞懂知识蒸馏概念
前端·后端·github
徐小夕3 小时前
实测 10 个前端 UI 生成开源 Skill|AI 输出直达上线标准
前端·vue.js·github
tong_li3 小时前
给娃做个 AI 陪写作业助手:LangChain4j 入门(八):AI 知道这字哪课学的(SQLite 结构化查询入门)
前端·langchain
用户69371750013843 小时前
当 Claude 开始反对蒸馏,我想起了那 700 万份盗版书籍
前端·后端·面试
zhanlang02174 小时前
HTML 零基础入门到企业实战全套教程【10.1】综合实战|从零开发静态企业官网:需求分析、页面结构规划、素材准备
前端·html
Bczheng14 小时前
浏览器访问本地html和服务器html的流程
前端
金八梭4 小时前
格斗对决v4.5
前端·css·自动化·html·css3
狮子座的男孩4 小时前
javascript 基础 es6:3、参数默认值,reset参数,spread扩展运算符,扩展运算符应用
前端·javascript·经验分享·es6·参数默认值·reset参数·spread扩展运算符
码间留白4 小时前
鸿蒙 Web 播放器断流不报错:黑屏提示从 70 秒优化到 2 秒
uni-app·鸿蒙·直播·arkweb·鸿蒙os开发