一个基于 Taro 3.x 的并集成 Tailwind CSS 和 Zustand 等技术栈的小程序开发模板。
当前仅支持适配微信、支付宝小程序。如果需要适配其他端,需自行安装 Taro 所需依赖。不建议使用该模板开发 RN 应用。
pnpm install
pnpm run dev:weapp
打开微信开发者工具,导入 weapp
目录并运行。
运行以下命令生成生产包:
pnpm run build:weapp
├── config/ # Taro 编译配置
├── src/
│ ├── constant/ # 常量定义
│ ├── components/ # 通用组件
│ ├── hooks/ # 自定义 Hooks
│ ├── store/ # Zustand 状态管理
│ ├── pages/ # 页面组件
│ ├── styles/ # 全局样式
│ ├── types/ # TypeScript 类型定义
│ ├── utils/ # 工具函数
│ │ ├── index.ts # 工具函数入口
│ │ └── request.ts # 请求封装
│ ├── app.config.ts # 应用配置
│ ├── app.less # 全局样式
│ ├── app.tsx # 应用入口
│ └── index.html # HTML 模板
├── types/ # 全局类型定义
│ └── global.d.ts
├── .editorconfig # 编码规范配置
├── .eslintrc # ESLint 配置
├── .gitignore # Git 忽略文件
├── babel.config.js # Babel 配置
├── postcss.config.js # PostCSS 配置
├── tailwind.config.js # Tailwind CSS 配置
├── tsconfig.json # TypeScript 配置
└── pnpm-lock.yaml # 依赖锁定文件
针对页面内容较多的情况,使用 Taro.nextTick()
延迟非首屏内容的渲染,减少白屏时间。
const NextTickComponent: React.FC = ({ children }) => {
const [isMounted, setIsMounted] = useState(false);
useEffect(() => {
Taro.nextTick(() => setIsMounted(true));
}, []);
return isMounted ? <>{children}</> : null;
};
export default NextTickComponent;
// 使用示例
<NextTickComponent>
<Content />
</NextTickComponent>
在页面跳转时提前加载数据,缩短目标页面的加载时间。
// A 页面
Taro.preload({
RequestPromise: getData(),
});
Taro.navigateTo({ url: '/pages/B/B' });
// B 页面
useEffect(() => {
Taro.getCurrentInstance().preloadData?.RequestPromise?.then((res) => {
// 更新状态
});
}, []);
lazyload
懒加载(注意:懒加载仅对三屏以外的图片生效)对于实时性要求较低的数据(如文章、商品展示),可以实现缓存优先策略。
复杂组件可使用 Taro 官方提供的 CustomWrapper
包裹,提升渲染性能。
<CustomWrapper>
<GoodsList />
</CustomWrapper>
支持微信、支付宝双端埋点集成。
src/utils/request.ts
提供了根据当前环境动态获取 API 地址的方法 getApiUrl
。
注意:暴露测试环境或开发环境地址可能带来安全风险,请谨慎使用。
@antmjs/vantui
的组件有时无法正确应用 Tailwind CSS 类名,请确保样式正确覆盖。dev
和build
命令后,需在微信开发者工具中清除缓存并重新编译。JS
转为ES5
,以提高兼容性。Swiper
组件,避免动画效果问题。Taro.showToast()
实现统一的 Toast 提示。