您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

关于微信小程序Redux绑定的解析

2024/6/4 9:58:14发布26次查看
这篇文章主要介绍了微信小程序redux绑定实例详解的相关资料,需要的朋友可以参考下
微信小程序redux绑定实例详解
安装
clone或者下载代码库到本地:
git clone https://github.com/charleyw/wechat-weapp-redux
将dist/wechat-weapp-redux.js(或者拷贝minify的也可以)文件直接拷贝到小程序的工程中,例如(下面假设我们把第三方包都安装在libs目录下):
cd wechat-weapp-redux cp -r dist/wechat-weapp-redux.js <小程序根目录>/libs
上面的命令将包拷贝到小程序的libs目录下
使用
1.将redux store绑定到app上。
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到页面上。
const pageconfig = { data: { }, ... }
页面的定义
const mapstatetodata = state => ({ todos: state.todos, visibilityfilter: state.visibilityfilter })
定义要映射哪些state到页面
const mapdispatchtopage = dispatch => ({ setvisibilityfilter: filter => dispatch(setvisibilityfilter(filter)), toggletodo: id => dispatch(toggletodo(id)), addtodo: text => dispatch(addtodo(text)), })
定义要映射哪些方法到页面
const nextpageconfig = connect(mapstatetodata, mapdispatchtopage)(pageconfig)
使用connect将上述定义添加到pageconfig中。
page(nextpageconfig);
注册小程序的页面
3.说明
完成上述两步之后,你就可以在this.data中访问你在mapstatetodata定义的数据了。
mapdispatchtopage定义的action会被映射到this对象上。
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注!
相关推荐:
微信小程序中ui与容器组件的介绍
关于微信小程序md5的方法的解析
微信小程序新增的拖动组件movable-view的使用介绍
以上就是关于微信小程序redux绑定的解析的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product