美文网首页
Vue3使用路由

Vue3使用路由

作者: 寻找无名的特质 | 来源:发表于2022-05-21 06:24 被阅读0次

首先定义路由,需要引入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 路由离开钩子

相关文章

网友评论

      本文标题:Vue3使用路由

      本文链接:https://www.haomeiwen.com/subject/birwurtx.html