Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dc35639

Browse files
committed
fix: import error
1 parent 7da4545 commit dc35639

File tree

7 files changed

+68
-75
lines changed

7 files changed

+68
-75
lines changed

src/store/dict.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { fetchDictList } from '@/service'
2+
import { session } from '@/utils'
3+
4+
export const useDictStore = defineStore('dict-store', {
5+
state: () => {
6+
return {
7+
dictMap: {} as DictMap,
8+
isInitDict: false,
9+
}
10+
},
11+
actions: {
12+
async dict(code: string) {
13+
// 调用前初始化
14+
if (!this.dictMap) {
15+
this.initDict()
16+
}
17+
18+
const targetDict = await this.getDict(code)
19+
20+
return {
21+
data: () => targetDict,
22+
enum: () => Object.fromEntries(targetDict.map(({ value, label }) => [value, label])),
23+
valueMap: () => Object.fromEntries(targetDict.map(({ value, ...data }) => [value, data])),
24+
labelMap: () => Object.fromEntries(targetDict.map(({ label, ...data }) => [label, data])),
25+
}
26+
},
27+
async getDict(code: string) {
28+
const isExist = Reflect.has(this.dictMap, code)
29+
30+
if (isExist) {
31+
return this.dictMap[code]
32+
}
33+
else {
34+
return await this.getDictByNet(code)
35+
}
36+
},
37+
38+
async getDictByNet(code: string) {
39+
const { data, isSuccess } = await fetchDictList(code)
40+
if (isSuccess) {
41+
Reflect.set(this.dictMap, code, data)
42+
// 同步至session
43+
session.set('dict', this.dictMap)
44+
return data
45+
}
46+
else {
47+
throw new Error(`Failed to get ${code} dictionary from network, check ${code} field or network`)
48+
}
49+
},
50+
initDict() {
51+
const dict = session.get('dict')
52+
if (dict) {
53+
Object.assign(this.dictMap, dict)
54+
}
55+
this.isInitDict = true
56+
},
57+
},
58+
})

src/store/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
33

44
export * from './app/index'
55
export * from './auth'
6+
export * from './dict'
67
export * from './router'
78
export * from './tab'
89

src/utils/dict.ts

Lines changed: 0 additions & 67 deletions
This file was deleted.

src/utils/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1+
export * from './storage'
12
export * from './array'
2-
export * from './dict'
33
export * from './i18n'
44
export * from './icon'
5-
export * from './storage'

src/views/demo/cascader/index.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,19 @@ function handleValidateClick() {
3333

3434
<div>
3535
<n-h2>回调value</n-h2>
36-
<pre class="bg-#eee">
36+
<pre class="bg-#eee:30">
3737
{{ cbValue }}
3838
</pre>
3939
</div>
4040
<div>
4141
<n-h2>回调option</n-h2>
42-
<pre class="bg-#eee">
42+
<pre class="bg-#eee:30">
4343
{{ cbOption }}
4444
</pre>
4545
</div>
4646
<div>
4747
<n-h2>回调pathValues</n-h2>
48-
<pre class="bg-#eee">
48+
<pre class="bg-#eee:30">
4949
{{ cbPathValues }}
5050
</pre>
5151
</div>

src/views/demo/dict/index.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<script setup lang="ts">
22
import { fetchDictList } from '@/service'
3-
import { dict } from '@/utils/dict'
3+
import { useDictStore } from '@/store'
4+
5+
const { dict } = useDictStore()
46
57
const dictKey = ref('')
68
const options = ref()
@@ -88,7 +90,7 @@ const enumLabel = computed(() => {
8890
</n-button>
8991
</n-flex>
9092

91-
<pre class="bg-#eee">
93+
<pre class="bg-#eee:30">
9294
{{ data }}
9395
</pre>
9496

src/views/demo/fetch/index.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function handleUpdate(data: any) {
4646
</div>
4747
</template>
4848
<template #2>
49-
<pre class="bg-#eee">
49+
<pre class="bg-#eee:30">
5050
{{ msg }}
5151
</pre>
5252
</template>

0 commit comments

Comments
 (0)