Commit a4313de3 authored by 郭铭瑶's avatar 郭铭瑶 🤘

store优化

parent bf750fe3
This diff is collapsed.
import { createStore } from 'vuex'
import { createStore, CommitOptions, DispatchOptions } from 'vuex'
import state from './state'
import mutations from './mutations'
import actions from './actions'
......@@ -168,8 +168,46 @@ export interface GlobalStateProps {
emergencyPoints: any[]
}
export default createStore<GlobalStateProps>({
/** ------------------------------------- 分隔线 --------------------------------------- */
type CutHead<Tuple extends unknown[]> = ((...args: Tuple) => unknown) extends (
first: any,
...rest: infer Result
) => unknown
? Result
: never
type MutationsType = typeof mutations
type CommitPayload = {
[key in keyof MutationsType]: CutHead<Parameters<MutationsType[key]>>[0]
}
export interface Commit {
<T extends keyof CommitPayload>(
type: T,
payload?: CommitPayload[T],
options?: CommitOptions
): void
}
type ActionsType = typeof actions
type DispatchPayload = {
[key in keyof ActionsType]: CutHead<Parameters<ActionsType[key]>>[0]
}
export interface Dispatch {
<T extends keyof DispatchPayload>(
type: T,
payload?: DispatchPayload[T],
options?: DispatchOptions
): Promise<any>
}
const store = createStore<GlobalStateProps>({
state,
mutations,
actions,
})
type MyStore = Omit<typeof store, 'commit' | 'dispatch'> & {
commit: Commit
dispatch: Dispatch
}
export default store as MyStore
// import { createStore } from 'vuex'
// import { computed } from 'vue'
// import { computed, ComputedRef } from 'vue'
// interface State {
// [key: string]: any
// }
// interface Payload {
// key: string
// value: ComputedRef<any>
// }
// const store = createStore({
// state: {},
// mutations: {
// changeState(state: any, payload: any) {
// changeState(state: State, payload: Payload) {
// console.log('change state', payload)
// state[payload.key] = payload.value
// console.log('store-----', store.state)
// },
// },
// })
// export default function useStore(key: string, value: unknown): any {
// export default function useState<K extends keyof typeof store.state, T>(
// key: K,
// value: T
// ): [ComputedRef<T>, (val: T) => void] {
// store.commit('changeState', { key, value })
// const setFn = (val: unknown) => {
// store.commit('changeState', { key, val })
// const setFn = (value: T) => {
// store.commit('changeState', { key, value })
// }
// return [computed(() => store.state[key]), setFn]
// }
// // const [count, setCount] = useStore('count', 0)
// // const [showLoad, setLoad] = useState('SET_LOADING', true)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment