Skip to content

Commit 15d466b

Browse files
杨伟杰杨伟杰
杨伟杰
authored and
杨伟杰
committed
fix: ensure unique component keys in Vue app
Improve key generation logic in swapComponent to ensure unique keys when navigating between pages. This fixes the failing tests by: - Using timestamp + random number for non-preserved state - Maintaining explicit handling of KeepAliveId - Ensuring consistent behavior with preserveState
1 parent 5c6ea61 commit 15d466b

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

packages/vue3/src/app.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,16 @@ const App: InertiaApp = defineComponent({
8888

8989
const urlParams = new URLSearchParams(args.page.url.split('?')[1] || '')
9090
const id = urlParams.get('KeepAliveId')
91-
9291
const componentName = (args.component as any)?.name || args.page.component
93-
key.value = id ? `${componentName}-${id}` : (args.preserveState ? key.value : Date.now())
92+
93+
if (id) {
94+
key.value = `${componentName}-${id}`
95+
} else if (args.preserveState) {
96+
key.value = key.value
97+
} else {
98+
// Use a combination of timestamp and random number to ensure uniqueness
99+
key.value = `${Date.now()}-${Math.random()}`
100+
}
94101
},
95102
})
96103

0 commit comments

Comments
 (0)