小程序中wepy-redux的使用以及存儲全局變量

這篇文章主要介紹了小程序中wepy-redux的使用以及存儲全局變量,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

成都創(chuàng)新互聯(lián)公司堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站設(shè)計、成都網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的富拉爾基網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

wepy里推薦使用wepy-redux存儲全局變量

使用

1.初始化store

// app.wpy
import { setStore } from 'wepy-redux'
import configStore from './store'

const store = configStore()
setStore(store) //setStore是將store注入到所有頁面中
// store文件夾下的index.js

import { createStore, applyMiddleware } from 'redux'
import promiseMiddleware from 'redux-promise'
import rootReducer from './reducers'

export default function configStore () {
  const store = createStore(rootReducer, applyMiddleware(promiseMiddleware)) //生成一個 store 對象
  return store
}

applyMiddleware 函數(shù)的作用就是對 store.dispatch 方法進(jìn)行增強(qiáng)和改造
這里就是使用redux-promise來解決異步

2.page里面獲取store


import { getStore } from 'wepy-redux'
 
const store = getStore()

// dispatch
store.dispatch({type: 'xx', payload: data}) //xx是reducer名字 payload就是攜帶的數(shù)據(jù)
store.dispatch(getAllHoomInfo(store.getState().base)) //xx是action名字

//獲取state
const state = store.getState()

3.連接組件

@connect({
    data:(state) => state.base.data //注意這里是base下的state 所有要加上base.
})

文件介紹

redux文件

小程序中wepy-redux的使用以及存儲全局變量

type

types里是觸發(fā)action的函數(shù)名稱 只是存儲函數(shù)名字

按照模塊去創(chuàng)建type.js

小程序中wepy-redux的使用以及存儲全局變量

//base.js
export const GETALLHOMEINFO = 'GETALLHOMEINFO'

寫好了函數(shù)名稱 在index.js中export出來

export * from './counter'
export * from './base'

reducers

隨著應(yīng)用變得復(fù)雜,需要對 reducer 函數(shù) 進(jìn)行拆分,拆分后的每一塊獨立負(fù)責(zé)管理 state 的一部分
這個時候多個模塊的reducer通過combineReducers合并成一個最終的 reducer 函數(shù),

小程序中wepy-redux的使用以及存儲全局變量

import { combineReducers } from 'redux'
import base from './base'
import counter from './counter'

export default combineReducers({
  base,
  counter
})

模塊使用handleActions來處理reducer,將多個相關(guān)的reducers寫在一起
handleActions有兩個參數(shù):第一個是多個reducers,第二個是初始state

GETALLHOMEINFO reducer是將異步action返回的值賦值給data

//base.js
import { handleActions } from 'redux-actions'
import { GETALLHOMEINFO } from '../types/base'

const initialState = {
  data: {}
}
export default handleActions({
  [GETALLHOMEINFO] (state, action) {
    return {
      ...state,
      data: action.payload
    }
  }
}, initialState)

actions

actions是對數(shù)據(jù)的處理
小程序中wepy-redux的使用以及存儲全局變量

在index.js中export出來

export * from './counter'
export * from './base'

createAction用來創(chuàng)建Action的

import { GETALLHOMEINFO } from '../types/base'
import { createAction } from 'redux-actions'
import { Http, Apis } from '../../libs/interface'

export const getAllHoomInfo = createAction(GETALLHOMEINFO, (base) => {
  return new Promise(async resolve => {
    let data = await Http.get({
      url: Apis.ls_url + Apis.allHomeInfo,
      data: {}
    })
    resolve(data)**//返回到reduer的action.payload**
  })
})

用法

<script>
  import wepy from 'wepy'
  import { connect } from 'wepy-redux'
  import { getAllHoomInfo } from '../store/actions/base.js'// 引入action方法
  import { getStore } from 'wepy-redux'
 
  const store = getStore()
  
  @connect({
    data:(state) => state.base.data
  })

  export default class Index extends wepy.page {
    data = {
    }

    computed = {
    }

    onLoad() {
      store.dispatch(getAllHoomInfo(store.getState().base))
    }
    
  }
</script>

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“小程序中wepy-redux的使用以及存儲全局變量”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

文章標(biāo)題:小程序中wepy-redux的使用以及存儲全局變量
本文網(wǎng)址:http://muchs.cn/article20/gheeco.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、企業(yè)網(wǎng)站制作、面包屑導(dǎo)航、App開發(fā)、自適應(yīng)網(wǎng)站、網(wǎng)頁設(shè)計公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)