WeChat Applet
Mini Program Development
The use of wepy-redux and storage of global variables in small programsThe use of wepy-redux and storage of global variables in small programs
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注入到所有页面中
// 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 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() 3. Connection component
@connect({
data:(state) => state.base.data //注意这里是base下的state 所有要加上base.
})
File introduction
redux file

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

//base.js export const GETALLHOMEINFO = 'GETALLHOMEINFO'
After writing the function name, export it in index.js
export * from './counter' export * from './base'
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,

import { combineReducers } from 'redux'
import base from './base'
import counter from './counter'
export default combineReducers({
base,
counter
})
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)
actions
actions is the processing of data
Exported in index.js
export * from './counter' export * from './base'
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**
})
})
Usage
<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>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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6
Visual web development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment





