Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
E
east-nanjing-new
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
郭铭瑶
east-nanjing-new
Commits
a4313de3
Commit
a4313de3
authored
Jun 25, 2021
by
郭铭瑶
🤘
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
store优化
parent
bf750fe3
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
120 additions
and
156 deletions
+120
-156
actions.ts
src/store/actions.ts
+59
-148
index.ts
src/store/index.ts
+40
-2
useStore.ts
src/store/useStore.ts
+21
-6
No files found.
src/store/actions.ts
View file @
a4313de3
This diff is collapsed.
Click to expand it.
src/store/index.ts
View file @
a4313de3
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
src/store/useStore.ts
View file @
a4313de3
// 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 = (value: T) => {
// store.commit('changeState', { key, value })
// const setFn = (val: unknown) => {
// store.commit('changeState', { key, val })
// }
// return [computed(() => store.state[key]), setFn]
// }
// // const [
count, setCount] = useStore('count', 0
)
// // const [
showLoad, setLoad] = useState('SET_LOADING', true
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment