微信小程序Redux绑定实例详解
安装
clone或者下载代码库到本地:
rush:bash;">
git clone https://github.com/charleyw/wechat-weapp-redux
将dist/wechat-weapp-redux.js(或者拷贝minify的也可以)文件直接拷贝到小程序的工程中,例如(下面假设我们把第三方包都安装在libs目录下):
上面的命令将包拷贝到小程序的libs目录下
使用
1.将Redux Store绑定到App上。
rush:bash;">
const store = createStore(reducer) // redux store
const WeAppRedux = require('./libs/wechat-weapp-redux/index.js');
const {Provider} = WeAppRedux;
Provider是用来把Redux的store绑定到App上。
rush:bash;">
App(Provider(store)({
onLaunch: function () {
console.log("onLaunch")
}
}))
provider的实现只是简单的将store加到App这个global对象上,方便在页面中用getApp取出来
上面这段代码等同于:
rush:bash;">
App({
onLaunch: function() {
console.log( "onLaunch" )
},store: store
})
2.在页面的定义上使用connect,绑定redux store到页面上。
rush:bash;">
const pageConfig = {
data: {
},...
}
页面的定义
定义要映射哪些state到页面
({
setVisibilityFilter: filter => dispatch(setVisibilityFilter(filter)),toggletodo: id => dispatch(toggletodo(id)),addTodo: text => dispatch(addTodo(text)),})
rush:js;">
Page(nextPageConfig);
3.说明
完成上述两步之后,你就可以在this.data中访问你在mapStatetoData定义的数据了。
mapdispatchToPage定义的action会被映射到this对象上。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。