一个基于 TypeScript + Less + TDesign 的微信小程序快速开发模板,支持云函数开发和 Skyline 渲染引擎。
weapp-template/ ├── miniprogram/ # 小程序前端 │ ├── components/ # 自定义组件 │ │ └── navigation-bar/ # 自定义导航栏组件 │ ├── pages/ # 页面文件 │ │ ├── index/ # 首页 │ │ ├── logs/ # 日志页 │ │ └── helloworld/ # 示例页 │ ├── utils/ # 工具函数 │ ├── app.ts # 应用入口 │ ├── app.json # 应用配置 │ └── app.less # 全局样式 ├── cloudfunctions/ # 云函数后端 │ └── minicloud/ # 云函数实现 │ ├── src/ # TypeScript 源码 │ └── dist/ # 编译输出 ├── typings/ # 类型定义 ├── project.config.json # 项目配置 └── tsconfig.json # TypeScript 配置
# 安装前端依赖
npm install
# 安装云函数依赖
cd cloudfunctions/minicloud
npm install
cd ../..
使用微信开发者工具打开项目
配置云开发环境
// miniprogram/app.ts
wx.cloud.init({
env: 'your-cloud-env-id', // 替换为你的云开发环境 ID
traceUser: true,
});
启动开发
cd cloudfunctions/minicloud
npm run build:watch
# 代码检查
npm run lint
# 自动修复代码问题
npm run lint:fix
# 格式化代码
npm run format
# 完整构建(推荐用于生产部署)
npm run build
# 快速开发构建
npm run build:dev
# 监听模式开发(支持热重载)
npm run build:watch
# 类型检查
npm run type-check
# 清理构建缓存
npm run clean
本模板集成了 TDesign 小程序组件库,提供丰富的企业级组件:
<!-- 在页面中使用 TDesign 组件 -->
<t-button type="primary" size="large">主要按钮</t-button>
<t-cell title="单元格" arrow />
<t-input placeholder="请输入内容" />
组件支持按需引入和懒加载:
// app.json 或页面 json 文件
{
"usingComponents": {
"t-button": "/miniprogram_npm/tdesign-miniprogram/button/button",
"t-cell": "/miniprogram_npm/tdesign-miniprogram/cell/cell"
}
}
# 在 miniprogram/components/ 目录下创建组件
mkdir miniprogram/components/my-component
组件结构:
my-component/ ├── my-component.ts # 组件逻辑 ├── my-component.wxml # 组件模板 ├── my-component.less # 组件样式 └── my-component.json # 组件配置
// my-component.ts
Component({
properties: {
title: {
type: String,
value: ''
}
},
data: {
// 组件数据
},
methods: {
// 组件方法
}
});
// cloudfunctions/minicloud/src/index.ts
interface CloudEvent extends Record<string, unknown> {
// 事件参数
}
interface CloudContext {
requestId: string;
functionName: string;
functionVersion: string;
}
// 云函数入口
exports.main = async (event: CloudEvent, context: CloudContext) => {
return {
success: true,
data: event
};
};
模板包含自适应导航栏组件,自动处理:
支持不同屏幕尺寸的适配:
使用 Husky + lint-staged 在提交前自动:
pages/ ├── module/ # 按功能模块划分 │ ├── list/ # 列表页面 │ ├── detail/ # 详情页面 │ └── edit/ # 编辑页面
// 简单状态使用 globalData
getApp().globalData.userInfo = userInfo;
// 复杂状态可以使用 Observable 模式
A: 检查 tsconfig.json 配置,确保包含所有必要的类型定义文件。
A: 确认 .less 文件已被微信开发者工具正确编译为 .wxss 文件。
A: 检查云函数代码是否通过 TypeScript 编译,dist 目录是否包含编译后的 js 文件。
A: 运行 npm install 后,在微信开发者工具中选择"工具 -> 构建 npm"。
欢迎提交 Issue 和 Pull Request 来完善这个模板!