34 lines
759 B
TypeScript
34 lines
759 B
TypeScript
|
import {createStore} from "redux"
|
||
|
// 设置数据
|
||
|
const baseState = {
|
||
|
msg:'嘻嘻嘻嘻嘻嘻',
|
||
|
num:10,
|
||
|
shuju:{
|
||
|
name:'小白',
|
||
|
hobby:'吃饭',
|
||
|
val:''
|
||
|
},
|
||
|
belongArray:[]
|
||
|
|
||
|
}
|
||
|
|
||
|
// 创建仓库
|
||
|
const reducer = (state = baseState,action:any)=>{
|
||
|
const nstate:any = JSON.parse(JSON.stringify(state))
|
||
|
if(action.type=="numadd"){
|
||
|
nstate.num++
|
||
|
// console.log('123');
|
||
|
}
|
||
|
if(action.type=='new'){
|
||
|
nstate.shuju.val = action.val
|
||
|
console.log(nstate.shuju.val);
|
||
|
}
|
||
|
// redux 要求 state必须深拷贝一次 才能返回
|
||
|
if(action.type == 'uparray'){
|
||
|
nstate.belongArray = action.val
|
||
|
}
|
||
|
return nstate
|
||
|
}
|
||
|
const store = createStore(reducer)
|
||
|
export default store
|