最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2017年06月07日 15:22:48   投稿:lqh  
這篇文章主要介紹了微信小程序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對象上。

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

相關文章

最新評論

大厂| 开江县| 莲花县| 浮山县| 乐至县| 中牟县| 五寨县| 阳朔县| 扬州市| 土默特左旗| 安宁市| 克什克腾旗| 安西县| 曲阳县| 宝应县| 定陶县| 德保县| 绿春县| 潮州市| 正蓝旗| 松江区| 高要市| 寿阳县| 长春市| 宁都县| 永年县| 舒兰市| 新乐市| 东乡| 滨海县| 江口县| 鱼台县| 聂拉木县| 姚安县| 库伦旗| 安乡县| 南靖县| 临桂县| 舞阳县| 闽清县| 广灵县|