這篇文章主要為大家展示了“微信小程序中Redux綁定的示例分析”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“微信小程序中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的實現(xiàn)只是簡單的將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定義的數(shù)據(jù)了。
mapDispatchToPage定義的action會被映射到this對象上。
以上是“微信小程序中Redux綁定的示例分析”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
標題名稱:微信小程序中Redux綁定的示例分析-創(chuàng)新互聯(lián)
文章URL:http://chinadenli.net/article20/hcico.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供微信小程序、Google、網(wǎng)頁設計公司、小程序開發(fā)、網(wǎng)站收錄、移動網(wǎng)站建設
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內容