vue3-router-tab 1.0.6 → 1.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/pinia.ts +59 -42
- package/package.json +2 -2
package/lib/pinia.ts
CHANGED
|
@@ -78,55 +78,72 @@ function createDefaultStore(options: RouterTabsPiniaOptions) {
|
|
|
78
78
|
* ```
|
|
79
79
|
*/
|
|
80
80
|
export function useRouterTabsPiniaPersistence(options: RouterTabsPiniaOptions = {}) {
|
|
81
|
-
const controller = useRouterTabs({ optional: true })
|
|
82
|
-
if (!controller) {
|
|
83
|
-
throw new Error('[RouterTabs] Pinia helper must be used inside <router-tab>.')
|
|
84
|
-
}
|
|
85
81
|
const Store = options.store ?? createDefaultStore(options)
|
|
86
82
|
const store = Store()
|
|
87
83
|
const hydrating = ref(false)
|
|
84
|
+
let initialized = false
|
|
88
85
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
if (snapshot && snapshot.tabs?.length) {
|
|
93
|
-
try {
|
|
94
|
-
hydrating.value = true
|
|
95
|
-
await controller.hydrate(snapshot)
|
|
96
|
-
} finally {
|
|
97
|
-
hydrating.value = false
|
|
98
|
-
}
|
|
99
|
-
} else {
|
|
100
|
-
try {
|
|
101
|
-
hydrating.value = true
|
|
102
|
-
const fallback = options.fallbackRoute ?? controller.options.defaultRoute
|
|
103
|
-
await controller.reset(fallback)
|
|
104
|
-
} finally {
|
|
105
|
-
hydrating.value = false
|
|
106
|
-
}
|
|
107
|
-
}
|
|
86
|
+
const attemptSetup = (controller: NonNullable<ReturnType<typeof useRouterTabs>>) => {
|
|
87
|
+
if (!controller || initialized) return
|
|
88
|
+
initialized = true
|
|
108
89
|
|
|
109
|
-
|
|
110
|
-
|
|
90
|
+
onMounted(async () => {
|
|
91
|
+
store.load()
|
|
92
|
+
const snapshot = store.snapshot
|
|
93
|
+
|
|
94
|
+
if (snapshot && snapshot.tabs?.length) {
|
|
95
|
+
try {
|
|
96
|
+
hydrating.value = true
|
|
97
|
+
await controller.hydrate(snapshot)
|
|
98
|
+
} finally {
|
|
99
|
+
hydrating.value = false
|
|
100
|
+
}
|
|
101
|
+
} else {
|
|
102
|
+
try {
|
|
103
|
+
hydrating.value = true
|
|
104
|
+
const fallback = options.fallbackRoute ?? controller.options.defaultRoute
|
|
105
|
+
await controller.reset(fallback)
|
|
106
|
+
} finally {
|
|
107
|
+
hydrating.value = false
|
|
108
|
+
}
|
|
109
|
+
}
|
|
111
110
|
|
|
112
|
-
watch(
|
|
113
|
-
() => ({
|
|
114
|
-
tabs: controller.tabs.map(tab => ({
|
|
115
|
-
to: tab.to,
|
|
116
|
-
title: tab.title,
|
|
117
|
-
tips: tab.tips,
|
|
118
|
-
icon: tab.icon,
|
|
119
|
-
tabClass: tab.tabClass,
|
|
120
|
-
closable: tab.closable
|
|
121
|
-
})),
|
|
122
|
-
active: controller.activeId.value
|
|
123
|
-
}),
|
|
124
|
-
() => {
|
|
125
|
-
if (hydrating.value) return
|
|
126
111
|
store.setSnapshot(controller.snapshot())
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
watch(
|
|
115
|
+
() => ({
|
|
116
|
+
tabs: controller.tabs.map(tab => ({
|
|
117
|
+
to: tab.to,
|
|
118
|
+
title: tab.title,
|
|
119
|
+
tips: tab.tips,
|
|
120
|
+
icon: tab.icon,
|
|
121
|
+
tabClass: tab.tabClass,
|
|
122
|
+
closable: tab.closable
|
|
123
|
+
})),
|
|
124
|
+
active: controller.activeId.value
|
|
125
|
+
}),
|
|
126
|
+
() => {
|
|
127
|
+
if (hydrating.value) return
|
|
128
|
+
store.setSnapshot(controller.snapshot())
|
|
129
|
+
},
|
|
130
|
+
{ deep: true }
|
|
131
|
+
)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const controller = useRouterTabs({ optional: true })
|
|
135
|
+
if (controller) {
|
|
136
|
+
attemptSetup(controller)
|
|
137
|
+
} else {
|
|
138
|
+
onMounted(() => {
|
|
139
|
+
const lateController = useRouterTabs({ optional: true })
|
|
140
|
+
if (lateController) {
|
|
141
|
+
attemptSetup(lateController)
|
|
142
|
+
} else if (import.meta.env?.DEV) {
|
|
143
|
+
console.warn('[RouterTabs] Pinia helper must be used inside <router-tab>.')
|
|
144
|
+
}
|
|
145
|
+
})
|
|
146
|
+
}
|
|
130
147
|
|
|
131
148
|
return store
|
|
132
149
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vue3-router-tab",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.8",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist",
|
|
@@ -23,12 +23,12 @@
|
|
|
23
23
|
"build": "vite build"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"pinia": "^3.0.3",
|
|
27
26
|
"vue": "^3.5.22",
|
|
28
27
|
"vue-router": "^4.5.1"
|
|
29
28
|
},
|
|
30
29
|
"devDependencies": {
|
|
31
30
|
"@vitejs/plugin-vue": "^6.0.1",
|
|
31
|
+
"pinia": "^3.0.3",
|
|
32
32
|
"sass": "^1.93.2",
|
|
33
33
|
"sass-loader": "^16.0.5",
|
|
34
34
|
"typescript": "^5.9.2",
|