写在前面: 从零开发一款自己的小程序 UI 组件库(一) https://www.cnblogs.com/e-cat/p/11260134.html
上节我们讲到初始化组件库模板, 模板文件概述, 模板上传 NPM 以及 NPM 包文件下载至本地并运用到项目. 这节我们继续, 内容主要有基础 UI 组件库的搭建 (button 组件的实例) 以及如何在本地使用 NPM link 调试 NPM 包项目.
本节所用到的物料: mineui-weapp 组件库 v1.1,weapp-for-mineui 程序 v1.1
1. 开发基础组件 button
我们上节有提到, 要开发组件库的话, 需要在官方单组件模板的基础上,1修改 tools 目录下的 config.JS 文件,2新建 src 下的 button 组件目录和 button 目录下的四个 index 文件以及3新建承载 button 组件的 button 页面
1tools 目录下的 config.JS 文件, 修改 entry: ['index'], 为: entry: ['index', 'button/index'].
2在 src 目录下新建 button 目录和 button 目录下的四个 index 文件[index.wxss,index.JSON,index.JS,index.wxml] , 并填充基础内容
修改 index.wxss 文件:
- @import "../common.wxss";
- .mine-button--default {
- color: #333;
- background-color: #fff;
- border: 1px solid #eee
- }
- .mine-button--primary {
- color: #fff;
- background-color: #07c160;
- border: 1px solid #07c160
- }
- .mine-button--info {
- color: #fff;
- background-color: #1989fa;
- border: 1px solid #1989fa
- }
- .mine-button--danger {
- color: #fff;
- background-color: #f44;
- border: 1px solid #f44
- }
- .mine-button--warning {
- color: #fff;
- background-color: #ff976a;
- border: 1px solid #ff976a
- }
修改 index.JSON 文件:
- {
- "component": true,
- "usingComponents": {}
- }
修改 index.JS 文件:
- Component({
- properties: {
- type: {
- type: String,
- value: 'primaty'
- }
- },
- methods: {
- onClick() {
- this.$emit('click')
- }
- }
- })
修改 index.wxml 文件:
- <button class="mine-button--{{type}}" bindtap="onClick">
- <slot />
- </button>
3新建承载 button 组件的 button 页面
修改在 tools/demo 目录下的 App.JSON 文件, pages 数组下增加 "pages/button/index" 的, 并在 tools/demo/pages 下添加 button 目录及其下的 index 文件, 这里修改 JSON,JS,wxml 文件
修改 index.JSON 文件:
- {
- "usingComponents": {
- "comp": "../../components/button"
- }
- }
修改 index.JS 文件:
Page({})
修改 index.wxml 文件:
<mine-button type="danger">I'm a button</mine-button>
在根目录下运行:
NPM run watch
使用微信开发者工具导入预览 miniprogram_dev 目录, 并将预览模式转到 "pages/button/index" 目录下, 即可看到一个红色的 button, 内容为: I'm a button
这时, 我们修改 tools/demo/pages/button/index.wxml 文件:
<mine-button type="primary">I'm a primary button</mine-button>
可以实时看到开发者工具预览按钮变成绿色的 button, 内容为: I'm a primary button
这里一个简单的 button 组件就编写完成了, 那么如何丰富它的属性让它适应更多的场景呢?
2. 丰富 button 组件
在上面的基础 button 组件中, 我们只配置了 button 的 type 即按钮的背景颜色和文字颜色. 但是通常我们使用到 button 时, 还会自定义按钮背景颜色, 按钮文字颜色及大小, 按钮的大小, 按钮的形状, 按钮的 loading 状态以及按钮的 open-type 等等, 这就需要我们修改 src/button 下的 index 文件了.
未完待续,,
从零开发一款自己的小程序 UI 组件库(二)
来源: http://www.bubuko.com/infodetail-3152949.html