2018-11-07 12:51:41 +08:00
|
|
|
|
# omi-mp - 微信小程序开发 Web
|
|
|
|
|
|
2018-11-09 10:15:32 +08:00
|
|
|
|
> 使用小程序生成 Web 的 H5 SPA (基于Omi + omi-router)
|
2018-11-09 10:14:59 +08:00
|
|
|
|
|
2018-11-09 10:13:08 +08:00
|
|
|
|
![](../../assets/mp1.jpg)
|
2018-11-07 12:51:41 +08:00
|
|
|
|
|
2018-11-09 10:13:08 +08:00
|
|
|
|
![](../../assets/mp2.jpg)
|
|
|
|
|
|
2018-11-09 10:16:41 +08:00
|
|
|
|
# 快速体验
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
npm start //开发
|
|
|
|
|
npm build //发布
|
|
|
|
|
```
|
|
|
|
|
|
2018-11-09 10:13:08 +08:00
|
|
|
|
# 原理
|
2018-11-07 12:51:41 +08:00
|
|
|
|
|
|
|
|
|
目前除了 template,其余基本都支持,不支持的欢迎反馈或 PR。
|
|
|
|
|
|
|
|
|
|
举个99乘法表的例子:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
compile(`
|
|
|
|
|
<view wx:for="{{[1, 2, 3, 4, 5, 6, 7, 8, 9]}}" wx:for-item="i">
|
|
|
|
|
<view wx:for="{{[1, 2, 3, 4, 5, 6, 7, 8, 9]}}" wx:for-item="j">
|
|
|
|
|
<view wx:if="{{i <= j}}">
|
|
|
|
|
{{i}} * {{j}} = {{i * j}}
|
|
|
|
|
</view>
|
|
|
|
|
</view>
|
|
|
|
|
</view>`)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
编译之后:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
function render() {
|
|
|
|
|
return (
|
|
|
|
|
h('view', {}, [1, 2, 3, 4, 5, 6, 7, 8, 9].map((i, index) => {
|
|
|
|
|
h('view', {}, [1, 2, 3, 4, 5, 6, 7, 8, 9].map((j, index) => {
|
|
|
|
|
i <= j && h('view', {}, [`${i} * ${j} = ${i * j}`])
|
|
|
|
|
}))
|
|
|
|
|
}))
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2018-11-09 09:40:34 +08:00
|
|
|
|
## 小程序生命周期
|
|
|
|
|
|
|
|
|
|
### 页面生命周期函数
|
|
|
|
|
|
|
|
|
|
| 名称 | 描述 |
|
|
|
|
|
| ------ | ------ |
|
2018-11-09 10:13:08 +08:00
|
|
|
|
| onLoad | 监听页面加载,对应 Omi installed |
|
2018-11-09 09:40:34 +08:00
|
|
|
|
| onShow | 监听页面显示 |
|
2018-11-09 10:13:08 +08:00
|
|
|
|
| onReady | 监听页面初次渲染完成 ,对应 Omi installed |
|
2018-11-09 09:40:34 +08:00
|
|
|
|
| onHide | 监听页面隐藏 |
|
2018-11-09 10:13:08 +08:00
|
|
|
|
| onUnload | 监听页面卸载 ,对应 Omi uninstall |
|
2018-11-09 09:40:34 +08:00
|
|
|
|
|
|
|
|
|
### 组件生命周期函数
|
|
|
|
|
|
|
|
|
|
| 名称 | 描述 |
|
|
|
|
|
| ------ | ------ |
|
2018-11-09 10:13:08 +08:00
|
|
|
|
| created | 在组件实例进入页面节点树时执行,注意此时不能调用 setData ,对应 Omi install |
|
|
|
|
|
| attached | 在组件实例进入页面节点树时执行 ,对应 Omi installed |
|
|
|
|
|
| ready | 在组件布局完成后执行,此时可以获取节点信息(使用 SelectorQuery ) ,对应 Omi installed |
|
2018-11-09 09:40:34 +08:00
|
|
|
|
| moved | 在组件实例被移动到节点树另一个位置时执行 |
|
2018-11-09 10:13:08 +08:00
|
|
|
|
| detached | 在组件实例被从页面节点树移除时执行 ,对应 Omi uninstall |
|
2018-11-09 09:40:34 +08:00
|
|
|
|
|
2018-11-07 12:51:41 +08:00
|
|
|
|
## License
|
|
|
|
|
MIT [@dntzhang](https://github.com/dntzhang)
|