粥里有勺糖

vuePress-theme-reco 粥里有勺糖    2018 - 2023
粥里有勺糖 粥里有勺糖

Choose mode

  • dark
  • auto
  • light
关于我
备战春秋
  • 心得总结
  • 校招考点汇总
  • 面经汇总
  • 复习自查
技术笔记
  • 技术教程
  • 模板工程
  • 源码学习
  • 技术概念
  • 个人作品
  • 学习笔记
计算机基础
  • 算法与数据结构
  • 操作系统
  • 计算机网络
  • 设计模式
  • 剑指offer
大前端
  • javascript
  • vue
  • html
  • css
  • 🌏浏览器专题
  • Web性能优化
  • regexp
  • node
面试
  • 问解
  • javascript
  • css
  • 手撕代码
  • 性能优化
  • 综合问题
  • 面经汇总
  • 小程序
手撕代码
  • 数据结构与算法
  • javascript
  • css
个人站点
  • GitHub (opens new window)
  • 博客园 (opens new window)
  • 掘金 (opens new window)
线上作品
  • 轻取(文件收集) (opens new window)
  • 个人图床 (opens new window)
  • 考勤小程序 (opens new window)
  • 时光恋人 (opens new window)
  • 在线简历生成 (opens new window)
留言板
Github (opens new window)
author-avatar

粥里有勺糖

285

文章

40

标签

关于我
备战春秋
  • 心得总结
  • 校招考点汇总
  • 面经汇总
  • 复习自查
技术笔记
  • 技术教程
  • 模板工程
  • 源码学习
  • 技术概念
  • 个人作品
  • 学习笔记
计算机基础
  • 算法与数据结构
  • 操作系统
  • 计算机网络
  • 设计模式
  • 剑指offer
大前端
  • javascript
  • vue
  • html
  • css
  • 🌏浏览器专题
  • Web性能优化
  • regexp
  • node
面试
  • 问解
  • javascript
  • css
  • 手撕代码
  • 性能优化
  • 综合问题
  • 面经汇总
  • 小程序
手撕代码
  • 数据结构与算法
  • javascript
  • css
个人站点
  • GitHub (opens new window)
  • 博客园 (opens new window)
  • 掘金 (opens new window)
线上作品
  • 轻取(文件收集) (opens new window)
  • 个人图床 (opens new window)
  • 考勤小程序 (opens new window)
  • 时光恋人 (opens new window)
  • 在线简历生成 (opens new window)
留言板
Github (opens new window)
  • Template Project

    • 工程模板
    • 模板工程搭建:Vue-Cli搭建Vue3/TS/uni-app小程序工程(上)
    • 模板工程搭建:Vue-Cli搭建Vue3/TS/uni-app小程序工程(中)
    • 模板工程搭建:Vue-Cli搭建Vue3/TS/uni-app小程序工程(下)
    • 开箱即用的Vite-Vue3工程化模板
    • 模板工程搭建:Web-SDK/Library

模板工程搭建:Vue-Cli搭建Vue3/TS/uni-app小程序工程(下)

vuePress-theme-reco 粥里有勺糖    2018 - 2023

模板工程搭建:Vue-Cli搭建Vue3/TS/uni-app小程序工程(下)

粥里有勺糖 2021-06-23 技术笔记工程模板

# 模板工程搭建:Vue-Cli搭建Vue3/TS/uni-app小程序工程(下)

# 前言

前两期:

  • 上
  • 中

已经搭建了一个包含了Vue3,TS,Sass,Vant Weapp,Vuex4,Axios,Eslint等特性的uni-vue3-ts (opens new window)工程化模板

本节将为模板接入更多的特性:

  • less
  • tailwindcss (opens new window)

# less

模板默认是没有支持less的

安装less-loader与less即可

需要指定版本,版本过高不支持低版本的webpack也无法顺利运行

yarn add less-loader@^7 less@^3.13.1 --dev
1

# tailwindcss

直接在 HTML 进行编写class即可实现页面样式生成

也就是前段时间刚出来时,社区讨论很激烈的一个东西

个人是不太喜欢这个东西,会导致样式与页面内容耦合度过高,仿佛回到了bootstrap时代

emm但如果拿来写一些简单的示例demo还是可以

优势就是对于不复杂的组价能直观的看到标签对应的最终样式,不用去样式文件中检索查看

下面介绍一下在uni-app中使用,咱根据文档 (opens new window)摸索前进

# 安装依赖

  • tailwindcss
  • postcss
  • autoprefixer
  • postcss-class-rename
yarn add tailwindcss@npm:@tailwindcss/postcss7-compat postcss@^7 autoprefixer@^9 postcss-class-rename --dev
1

依赖安装完后就简单配置一下

# 配置

根目录下创建tailwind.config.js文件,兼容小程序部分的配置来源于文末的参考资料

module.exports = {
  purge: ['./src/**/*.{vue,js,ts,jsx,tsx}'],
  darkMode: false, // or 'media' or 'class'
  separator: '__', // 兼容小程序,将 : 替换成 __
  theme: {
    extend: {},
  },
  variants: {
    extend: {},
  },
  plugins: [],
  corePlugins: {
    // 兼容小程序,将带有 * 选择器的插件禁用
    preflight: false,
    space: false,
    divideColor: false,
    divideOpacity: false,
    divideStyle: false,
    divideWidth: false,
  },
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

修改postcss.config.js文件内容,最终配置如下

const path = require('path');

module.exports = {
  parser: require('postcss-comment'),
  plugins: [
    require('postcss-import')({
      resolve(id, basedir, importOptions) {
        if (id.startsWith('~@/')) {
          return path.resolve(process.env.UNI_INPUT_DIR, id.substr(3));
        }
        if (id.startsWith('@/')) {
          return path.resolve(process.env.UNI_INPUT_DIR, id.substr(2));
        }
        if (id.startsWith('/') && !id.startsWith('//')) {
          return path.resolve(process.env.UNI_INPUT_DIR, id.substr(1));
        }
        return id;
      },
    }),
    // 新增
    require('tailwindcss'),
    require('autoprefixer')({
      remove: process.env.UNI_PLATFORM !== 'h5',
    }),
    // 新增
    require('postcss-class-rename')({
      '\\\\.': '_', // 兼容小程序,将类名带 .和/ 替换成 _
    }),
    require('@dcloudio/vue-cli-plugin-uni/packages/postcss'),
  ],
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

在 App.vue中加入引入 tailwindcss的代码

<style>
/* tailwindcss */
@import 'tailwindcss/tailwind.css';
</style>
1
2
3
4

# 页面中使用

这里参考文档 (opens new window)用了几个简单的属性

<view>
  <text class="text-xl font-bold text-red-500">tailwindcss</text>
</view>
1
2
3

渲染结果如下

图片

# 最后

到这一个包含了常见特性的uni-app 开发小程序的工程化模板就算结束了,如果还要有推荐的特性欢迎评论交流

# 资料汇总

  • uni-vue3-ts:模板仓库 (opens new window)
  • PostCSS 7 compatibility build (opens new window)
  • uni-app 使用 tailwindcss (opens new window)
Edit this page (opens new window)
Last Updated: 2022/5/15 12:46:34