Skip to content

Commit 539d16f

Browse files
committed
fix: restore useRoute and useRouter Nuxt composables
resolves #43
1 parent a0e18ca commit 539d16f

File tree

2 files changed

+10
-30
lines changed

2 files changed

+10
-30
lines changed

src/parts/router.ts

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { existsSync } from 'node:fs'
2-
import { useNuxt, useLogger, addPlugin } from '@nuxt/kit'
2+
import { useNuxt, useLogger } from '@nuxt/kit'
33
import { join, resolve } from 'pathe'
44
import { runtimeDir } from '../utils'
55

@@ -21,7 +21,6 @@ export const setupRouter = () => {
2121
return
2222
}
2323

24-
addPlugin(resolve(runtimeDir, 'router'))
2524
nuxt.options.vite.optimizeDeps = nuxt.options.vite.optimizeDeps || {}
2625
nuxt.options.vite.optimizeDeps.include = nuxt.options.vite.optimizeDeps.include || []
2726
nuxt.options.vite.optimizeDeps.include.push('@ionic/vue-router')
@@ -31,32 +30,13 @@ export const setupRouter = () => {
3130
app.plugins = app.plugins.filter(
3231
p => !p.src.match(/nuxt3?\/dist\/(app\/plugins|pages\/runtime)\/router/)
3332
)
33+
app.plugins.unshift({
34+
src: resolve(runtimeDir, 'router'),
35+
mode: 'all',
36+
})
3437
})
3538
})
3639

37-
// Remove Nuxt useRoute & useRouter composables
38-
nuxt.hook('autoImports:sources', sources => {
39-
for (const source of sources) {
40-
if (source.from === '#app') {
41-
source.imports = source.imports.filter(
42-
i => typeof i !== 'string' || !['useRoute', 'useRouter'].includes(i)
43-
)
44-
}
45-
}
46-
sources.push({
47-
from: 'vue-router',
48-
imports: ['useRouter', 'useRoute'],
49-
})
50-
})
51-
52-
// Remove vue-router types
53-
nuxt.hook('prepare:types', ({ references }) => {
54-
const index = references.findIndex(i => 'types' in i && i.types === 'vue-router')
55-
if (index !== -1) {
56-
references.splice(index, 1)
57-
}
58-
})
59-
6040
// Add default ionic root layout
6141
nuxt.hook('app:resolve', app => {
6242
if (

src/runtime/router.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createRouter, createWebHistory, createMemoryHistory } from '@ionic/vue-router'
22

3-
import { computed, reactive, shallowRef } from 'vue'
3+
import { computed, ComputedRef, reactive, shallowRef } from 'vue'
44
import { createWebHashHistory, NavigationGuard, RouteLocation } from 'vue-router'
55
import { createError } from 'h3'
66
import { withoutBase, isEqual } from 'ufo'
@@ -72,9 +72,9 @@ export default defineNuxtPlugin(async nuxtApp => {
7272
})
7373

7474
// https://github.com/vuejs/router/blob/main/packages/router/src/router.ts#L1225-L1233
75-
const route = {} as { [K in RouteLocation]: ComputedRef<RouteLocation[K]> }
75+
const route = {} as { [K in keyof RouteLocation]: ComputedRef<RouteLocation[K]> }
7676
for (const key in _route.value) {
77-
route[key as keyof RouteLocation] = computed(() => _route.value[key as keyof RouteLocation])
77+
route[key as 'path'] = computed(() => _route.value[key as 'path'])
7878
}
7979

8080
nuxtApp._route = reactive(route)
@@ -86,7 +86,7 @@ export default defineNuxtPlugin(async nuxtApp => {
8686

8787
const error = useError()
8888

89-
const initialLayout = useState('_layout')
89+
const initialLayout = useState<string>('_layout')
9090
router.beforeEach(async (to, from) => {
9191
to.meta = reactive(to.meta)
9292
if (nuxtApp.isHydrating) {
@@ -117,7 +117,7 @@ export default defineNuxtPlugin(async nuxtApp => {
117117
const middleware =
118118
typeof entry === 'string'
119119
? nuxtApp._middleware.named[entry] ||
120-
(await namedMiddleware[entry]?.().then((r: any) => r.default || r))
120+
(await namedMiddleware[entry]?.().then((r: any) => r.default || r))
121121
: entry
122122

123123
if (!middleware) {

0 commit comments

Comments
 (0)