2021-01-18 12:46:19 +00:00
|
|
|
import { shallowRef } from 'vue'
|
|
|
|
import {
|
|
|
|
createRouter,
|
|
|
|
createWebHistory,
|
|
|
|
createMemoryHistory,
|
|
|
|
RouterLink
|
|
|
|
} from 'vue-router'
|
2021-03-29 09:40:51 +00:00
|
|
|
import type { Plugin } from '@nuxt/app'
|
2021-02-19 14:21:50 +00:00
|
|
|
import routes from 'nuxt/build/routes'
|
2021-01-18 12:46:19 +00:00
|
|
|
import NuxtPage from './NuxtPage.vue'
|
|
|
|
|
|
|
|
export default <Plugin> function router (nuxt) {
|
|
|
|
const { app } = nuxt
|
|
|
|
|
|
|
|
app.component('NuxtPage', NuxtPage)
|
|
|
|
app.component('NuxtLink', RouterLink)
|
|
|
|
|
|
|
|
const routerHistory = process.client
|
|
|
|
? createWebHistory()
|
|
|
|
: createMemoryHistory()
|
|
|
|
|
|
|
|
const router = createRouter({
|
|
|
|
history: routerHistory,
|
|
|
|
routes
|
|
|
|
})
|
|
|
|
app.use(router)
|
2021-02-19 01:08:45 +00:00
|
|
|
nuxt.provide('router', router)
|
2021-01-18 12:46:19 +00:00
|
|
|
|
|
|
|
const previousRoute = shallowRef(router.currentRoute.value)
|
|
|
|
router.afterEach((_to, from) => {
|
|
|
|
previousRoute.value = from
|
|
|
|
})
|
|
|
|
|
|
|
|
Object.defineProperty(app.config.globalProperties, 'previousRoute', {
|
|
|
|
get: () => previousRoute.value
|
|
|
|
})
|
|
|
|
|
2021-02-19 01:08:45 +00:00
|
|
|
nuxt.hook('app:created', async () => {
|
2021-01-18 12:46:19 +00:00
|
|
|
if (process.server) {
|
|
|
|
router.push(nuxt.ssrContext.url)
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await router.isReady()
|
|
|
|
if (!router.currentRoute.value.matched.length) {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|