Mina Component
小程序组件库
介绍
Mina Component 是一个微信 小程序组件库。
预览
扫描下方小程序二维码,体验组件库示例:
安装
步骤一 通过 npm 安装
使用 npm 构建前,请先阅读微信官方的 npm 支持
# 通过 npm 安装
npm i mina-component -S --production
# 通过 yarn 安装
yarn add mina-component --production
# 安装 0.x 版本
npm i mina-component -S --production
步骤二 构建 npm 包
打开微信开发者工具,点击 工具 -> 构建 npm,并勾选 使用 npm 模块 选项,构建完成后,即可引入组件。
步骤五 typescript 支持
如果你使用 typescript 开发小程序,还需要做如下操作,以获得顺畅的开发体验。
安装 miniprogram-api-typings
# 通过 npm 安装
npm i -D miniprogram-api-typings
# 通过 yarn 安装
yarn add -D miniprogram-api-typings
在 tsconfig.json 中增加如下配置,以防止 tsc 编译报错。
请将path/to/node_modules/mina-component
修改为项目的 node_modules
中 mina-component 所在的目录。
{
...
"compilerOptions": {
...
"baseUrl": ".",
"types": ["miniprogram-api-typings"],
"paths": {
"mina-component/*": ["path/to/node_modules/mina-component/dist/*"]
},
"lib": ["ES6"]
}
}
使用
引入组件
以 ImagePreview 组件为例,只需要在app.json
或index.json
中配置 ImagePreview 对应的路径即可。
所有组件文档中的引入路径均以 npm 安装为例,如果你是通过下载源代码的方式使用 mina-component,请将路径修改为项目中 mina-component 所在的目录。
// 通过 npm 安装
// app.json
"usingComponents": {
"mina-image-preview": "mina-component/image-preview/index"
}
// 通过下载源码使用 es6版本
// app.json
"usingComponents": {
"mina-image-preview": "path/to/mina-component/dist/image-preview/index"
}
// 通过下载源码使用 es5版本
// app.json
"usingComponents": {
"mina-image-preview": "path/to/mina-component/lib/image-preview/index"
}
使用组件
引入组件后,可以在 wxml 中直接使用组件
<mina-image-preview
id="J-preview"
catch:onClose="handleClosePreview"
catch:onChange="handlePreviewChange"
/>
其他
在开发者工具中预览示例小程序
# 将项目克隆到本地
git clone git@github.com:yw0525/mina-component.git
# 安装项目依赖
cd mina-component && npm install
# 执行组件编译
npm run dev
接着打开微信开发者工具,导入example
目录的项目就可以预览示例了。
开源协议
本项目基于 MIT 协议,请自由地享受和参与开源。