Home > WeChat Applet > Mini Program Development > The use of wepy-redux and storage of global variables in small programs

The use of wepy-redux and storage of global variables in small programs

hzc
Release: 2020-06-20 11:06:39
forward
3112 people have browsed it

Wepy recommends using wepy-redux to store global variables

Use

1. Initialize store

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

const store = configStore()
setStore(store) //setStore是将store注入到所有页面中
Copy after login
// 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
}
Copy after login

applyMiddleware The function of the function is to enhance and transform the store.dispatch method
Here is to use redux-promise to solve the asynchronous problem

2.page to get the store


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

// dispatch
store.dispatch({type: 'xx', payload: data}) //xx是reducer名字 payload就是携带的数据
store.dispatch(getAllHoomInfo(store.getState().base)) //xx是action名字

//获取state
const state = store.getState()
Copy after login

3. Connection component

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

File introduction

redux file

The use of wepy-redux and storage of global variables in small programs

type

Types is the name of the function that triggers the action. It just stores the function name.

Create type.js according to the module

The use of wepy-redux and storage of global variables in small programs

//base.js
export const GETALLHOMEINFO = 'GETALLHOMEINFO'
Copy after login

After writing the function name, export it in index.js

export * from './counter'
export * from './base'
Copy after login
Copy after login

reducers

Follow As the application becomes more complex, the reducer function needs to be split. Each piece after the split is independently responsible for managing a part of the state.
At this time, the reducers of multiple modules are combined into a final reducer through combineReducers Function,

The use of wepy-redux and storage of global variables in small programs

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

export default combineReducers({
  base,
  counter
})
Copy after login

module uses handleActions to process the reducer, and writes multiple related reducers together
handleActions There are two parameters: the first is multiple reducers, and the second is the initial state

GETALLHOMEINFO reducer assigns the value returned by the asynchronous action to 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)
Copy after login

actions

actions is the processing of data
The use of wepy-redux and storage of global variables in small programs

Exported in index.js

export * from './counter'
export * from './base'
Copy after login
Copy after login

createAction is used to create 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**
  })
})
Copy after login

Usage

<script>
  import wepy from &#39;wepy&#39;
  import { connect } from &#39;wepy-redux&#39;
  import { getAllHoomInfo } from &#39;../store/actions/base.js&#39;// 引入action方法
  import { getStore } from &#39;wepy-redux&#39;
 
  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>
Copy after login

Recommended tutorial: "WeChat Mini Program"

The above is the detailed content of The use of wepy-redux and storage of global variables in small programs. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template