• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

Redux绑定小程序-微信小程序源码

小程序 开心洋葱 2968次浏览 0个评论

Redux绑定微信小程序
==============
用于在微信小程序为页面绑定Redux Store。

百度网盘分享链接链接: https://pan.baidu.com/s/1skCH4rb 密码:

注意:本段内容须成功“回复本文”后“刷新本页”方可查看!


_PS: 代码是基于[react-redux](https://github.com/reactjs/react-redux)修改的_

## 安装
1. clone或者下载代码库到本地:

“`shell
git clone https://github.com/charleyw/wechat-weapp-redux
“`
2. 将`dist/wechat-weapp-redux.js`(或者拷贝minify的也可以)文件直接拷贝到小程序的工程中,例如(下面假设我们把第三方包都安装在libs目录下):

“` shell
cd wechat-weapp-redux
cp -r dist/wechat-weapp-redux.js <小程序根目录>/libs
“`
上面的命令将包拷贝到小程序的`libs`目录下

## 使用
1. 将Redux Store绑定到App上。

“`js
const store = createStore(reducer) // redux store

const WeAppRedux = require(‘./libs/wechat-weapp-redux/index.js’);
const {Provider} = WeAppRedux;

“`
**Provider**是用来把Redux的store绑定到App上。

“`
App(Provider(store)({
onLaunch: function () {
console.log(“onLaunch”)
}
}))
“`
provider的实现只是简单的将store加到App这个global对象上,方便在页面中用getApp取出来

上面这段代码等同于:
“`
App({
onLaunch: function() {
console.log( “onLaunch” )
},
store: store
})
“`
2. 在页面的定义上使用connect,绑定redux store到页面上。

“`js
const pageConfig = {
data: {
},

}

“`
页面的定义

“`js
const mapStateToData = state => ({
todos: state.todos,
visibilityFilter: state.visibilityFilter
})
“`
定义要映射哪些state到页面

“`js
const mapDispatchToPage = dispatch => ({
setVisibilityFilter: filter => dispatch(setVisibilityFilter(filter)),
toggleTodo: id => dispatch(toggleTodo(id)),
addTodo: text => dispatch(addTodo(text)),
})
“`
定义要映射哪些方法到页面

“`js
const nextPageConfig = connect(mapStateToData, mapDispatchToPage)(pageConfig)
“`
使用connect将上述定义添加到pageConfig中。

“`js
Page(nextPageConfig);
“`
注册小程序的页面

3. 说明

完成上述两步之后,你就可以在`this.data`中访问你在`mapStateToData`定义的数据了。

`mapDispatchToPage`定义的action会被映射到`this`对象上。

## Example

详细的使用例子可以参照: [wechat-weapp-redux-todos](https://github.com/charleyw/wechat-weapp-redux-todos)

真机实测版请clone下面这个repo,用小程序开发工具开启预览:
“`
git clone -b release https://github.com/charleyw/wechat-weapp-redux-todos.git
“`


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明Redux绑定小程序-微信小程序源码
喜欢 (0)

您必须 登录 才能发表评论!

加载中……