Skip to content

Commit 9119a06

Browse files
committed
feat: release 1.0
1 parent 4c6637c commit 9119a06

File tree

14 files changed

+250
-0
lines changed

14 files changed

+250
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
## miniprgram-mixin
2+
3+
As we know, in the miniprogram, `Component` has [`behaviors`](https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/behaviors.html) to reuse the same logics.But if we want to reuse these logics in `Page`, there is no solution.
4+
5+
`miniprogram-mixin` works.
6+
7+
8+
### install
9+
10+
```bash
11+
yarn add miniprogram-mixin
12+
```
13+
14+
### demo usage
15+
16+
- in the `/test/demo` directory
17+
18+
- You can also open the [code snippet link](https://developers.weixin.qq.com/s/BtaZPYmQ7Fbu) in wechat devtools.
19+
20+
#### LICENSE
21+
22+
[MIT](./LICENSE)

package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "miniprogram-mixin",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"repository": "https://github.com/UZIhuhuhu/mini-program-mixin.git",
6+
"author": "tinyhuang <[email protected]>",
7+
"license": "MIT"
8+
}

src/index.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* // 1. 在 app.js 中引入此文件
3+
* require('mixin.js')
4+
*
5+
* // 2. 撰写一个 testMixin.js
6+
* module.exports = {
7+
* data: { someData: 'testMixin' },
8+
* onShow () { console.log('Log from mixin!') }
9+
* }
10+
*
11+
* // 3. 在页面 page/index/index.js 引入
12+
* const testMixin = require('testMixin.js')
13+
* Page({
14+
* mixins: [testMixin]
15+
* })
16+
*/
17+
18+
const originPage = Page;
19+
const originComponent = Component;
20+
21+
const originProperties = ['data', 'properties', 'options'];
22+
const originMethods = [
23+
'onLoad',
24+
'onReady',
25+
'onShow',
26+
'onHide',
27+
'onUnload',
28+
'onPullDownRefresh',
29+
'onReachBottom',
30+
'onShareAppMessage',
31+
'onPageScroll',
32+
'onTabItemTap'
33+
];
34+
35+
function merge(mixins, config) {
36+
if (!Array.isArray(mixins)) return;
37+
mixins.forEach(mixinItem => {
38+
if (Object.prototype.toString.call(mixinItem) !== '[object Object]') {
39+
throw new Error(`mixin type must be Object`);
40+
}
41+
for (let [key, value] of Object.entries(mixinItem)) {
42+
if (originProperties.includes(key)) {
43+
config[key] = { ...value, ...config[key] };
44+
} else if (originMethods.includes(key)) {
45+
const originFunction = config[key];
46+
config[key] = function(...args) {
47+
value.call(this, ...args);
48+
return originFunction && originFunction.call(this, ...args);
49+
};
50+
} else {
51+
config = { ...config, ...mixinItem };
52+
}
53+
}
54+
});
55+
}
56+
57+
Page = config => {
58+
if (Array.isArray(config.mixins)) {
59+
config = merge(config, config.mixins);
60+
}
61+
originPage(config);
62+
};
63+
64+
Component = config => {
65+
if (Array.isArray(config.mixins)) {
66+
config = merge(config, config.mixins);
67+
}
68+
originComponent(config);
69+
};

test/demo/app.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require('./mixin')
2+
3+
App({
4+
onLaunch: function () {
5+
6+
}
7+
})

test/demo/app.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"pages": [
3+
"index/index"
4+
],
5+
"window": {
6+
"backgroundTextStyle": "light",
7+
"navigationBarBackgroundColor": "#fff",
8+
"navigationBarTitleText": "WeChat",
9+
"navigationBarTextStyle": "black"
10+
},
11+
"sitemapLocation": "sitemap.json"
12+
}

test/demo/app.wxss

Whitespace-only changes.

test/demo/index/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const app = getApp()
2+
const mixinA = require('../mixins/mixin')
3+
Page({
4+
data: {
5+
6+
},
7+
mixins: [mixinA],
8+
onLoad: function () {
9+
console.log('代码片段是一种迷你、可分享的小程序或小游戏项目,可用于分享小程序和小游戏的开发经验、展示组件和 API 的使用、复现开发问题和 Bug 等。可点击以下链接查看代码片段的详细文档:')
10+
console.log('https://mp.weixin.qq.com/debug/wxadoc/dev/devtools/devtools.html')
11+
},
12+
})

test/demo/index/index.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"usingComponents": {}
3+
}

test/demo/index/index.wxml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<view class="intro">欢迎使用代码片段,可在控制台查看代码片段的说明和文档</view>

test/demo/index/index.wxss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.intro {
2+
margin: 30px;
3+
text-align: center;
4+
}

0 commit comments

Comments
 (0)