【移动应用开发技术】小程序中wepy-redux的使用以及存储全局变量_第1页
【移动应用开发技术】小程序中wepy-redux的使用以及存储全局变量_第2页
【移动应用开发技术】小程序中wepy-redux的使用以及存储全局变量_第3页
【移动应用开发技术】小程序中wepy-redux的使用以及存储全局变量_第4页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

【移动应用开发技术】小程序中wepy-redux的使用以及存储全局变量

这篇文章主要介绍了小程序中wepy-redux的使用以及存储全局变量,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让在下带着大家一起了解一下。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函数的作用就是对store.dispatch方法进行增强和改造这里就是使用redux-promise来解决异步2.page里面获取storeimport

{

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.连接组件@connect({

data:(state)

=>

state.base.data

//注意这里是base下的state

所有要加上base.

})文件介绍redux文件typetypes里是触发action的函数名称只是存储函数名字按照模块去创建type.js//base.js

export

const

GETALLHOMEINFO

=

'GETALLHOMEINFO'写好了函数名称在index.js中export出来export

*

from

'./counter'

export

*

from

'./base'reducers随着应用变得复杂,需要对reducer函数进行拆分,拆分后的每一块独立负责管理state的一部分这个时候多个模块的reducer通过combineReducers合并成一个最终的reducer函数,import

{

combineReducers

}

from

'redux'

import

base

from

'./base'

import

counter

from

'./counter'

export

default

combineReducers({

base,

counter

})模块使用handleActions来处理reducer,将多个相关的reducers写在一起handleActions有两个参数:第一个是多个reducers,第二个是初始stateGETALLHOMEINFOreducer是将异步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)actionsactions是对数据的处理在index.js中export出来export

*

from

'./counter'

export

*

from

'./base'createAction用来创建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

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论