add react版前端
This commit is contained in:
54
web-react/src/store/index.js
Normal file
54
web-react/src/store/index.js
Normal file
@@ -0,0 +1,54 @@
|
||||
import { createStore } from 'redux'
|
||||
import { cloneDeep, result, isEqual } from 'lodash'
|
||||
import reducer from './reducer'
|
||||
|
||||
const store = createStore(reducer)
|
||||
|
||||
const exStore = cloneDeep(store)
|
||||
|
||||
/**
|
||||
* 允许传入第一个参数path,只监听指定属性路径的对象
|
||||
* @param {...any} args
|
||||
* @returns
|
||||
*/
|
||||
exStore.subscribe = (...args) => {
|
||||
let path,
|
||||
listener,
|
||||
snapshot
|
||||
|
||||
if (typeof args[0] === 'string' && typeof args[1] === 'function') {
|
||||
path = args[0]
|
||||
listener = args[1]
|
||||
snapshot = cloneDeep(result(store.getState(), path))
|
||||
} else {
|
||||
listener = args[0]
|
||||
}
|
||||
|
||||
return store.subscribe((...args) => {
|
||||
const state = store.getState()
|
||||
if (path) {
|
||||
const resultState = cloneDeep(result(state, path))
|
||||
if (!isEqual(snapshot, resultState)) {
|
||||
listener.apply(this, [...args, resultState])
|
||||
}
|
||||
snapshot = resultState
|
||||
} else {
|
||||
listener.apply(this, [...args, state])
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 允许传入参数path,只返回指定属性路径的对象
|
||||
* @param {*} path
|
||||
* @returns
|
||||
*/
|
||||
exStore.getState = (path) => {
|
||||
if (path) {
|
||||
return result(store.getState(), path)
|
||||
}
|
||||
|
||||
return store.getState()
|
||||
}
|
||||
|
||||
export default exStore
|
||||
28
web-react/src/store/reducer.js
Normal file
28
web-react/src/store/reducer.js
Normal file
@@ -0,0 +1,28 @@
|
||||
import { combineReducers } from 'redux'
|
||||
|
||||
const user = (state = {}, action) => {
|
||||
// 写入各种action对应的操作
|
||||
switch (action.type) {
|
||||
case 'SET_USER_ACCOUNT':
|
||||
const _state = { ...state, ...action.user }
|
||||
return _state
|
||||
default:
|
||||
return state
|
||||
}
|
||||
}
|
||||
|
||||
const dicData = (state = {}, action) => {
|
||||
switch (action.type) {
|
||||
case 'ADD_DIC_DATA':
|
||||
return state
|
||||
default:
|
||||
return state
|
||||
}
|
||||
}
|
||||
|
||||
const combine = combineReducers({
|
||||
user,
|
||||
dicData
|
||||
})
|
||||
|
||||
export default combine
|
||||
Reference in New Issue
Block a user