首先定义路由,需要引入createRouter和createWebHistory:
import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: [
{
path: '/',
name: 'home',
component: HomeView
},
{
path: '/about',
name: 'about',
// route level code-splitting
// this generates a separate chunk (About.[hash].js) for this route
// which is lazy-loaded when the route is visited.
component: () => import('../views/AboutView.vue')
}
]
})
export default router
在项目入口,引入路由:
import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'
import router from './router'
const app = createApp(App)
app.use(createPinia())
app.use(router)
app.mount('#app')
其它相关:
history: createWebHashHistory(process.env.BASE_URL), hash 路由
createWebHistory history 路由
createMemoryHistory 带缓存 history 路由
parseQuery 查询参数反序列化
stringifyQuery 查询参数序列化
onBeforeRouteLeave 路由离开钩子
网友评论