国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 熱點 > 微信 > 正文

微信小程序Redux綁定實例詳解

2024-07-22 01:17:03
字體:
來源:轉載
供稿:網友

微信小程序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對象上。

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 贞丰县| 毕节市| 牙克石市| 土默特左旗| 东阿县| 英吉沙县| 英德市| 文昌市| 手机| 富蕴县| 东乡县| 慈利县| 宜春市| 克什克腾旗| 嫩江县| 青川县| 庄浪县| 肃北| 陇南市| 汽车| 眉山市| 英德市| 灌云县| 正定县| 雷山县| 星子县| 香港 | 聂拉木县| 和田市| 江永县| 朝阳市| 出国| 深州市| 大英县| 阿瓦提县| 中宁县| 榕江县| 淮安市| 济源市| 阜南县| 江油市|