安装 | Vue Router
npm install vue-router@4
// src/router/index.js import { createRouter, createWebHashHistory } from 'vue-router' // 2.定义一些路由 const routes = [] // 3.创建路由实例并传递 `routes` 配置 const router = createRouter({ history: createWebHashHistory(), routes }) // 4.暴露出去 export default router
应用到项目入口文件 main.js
import router from './router' '''略 app.use(router)
// 自定义路由读取路径配置: vite.config.js import path from 'path' // + export default defineConfig({ resolve: { alias: { "~": path.resolve(__dirname,"src") // + } }, ...略 })
pages/index.vue 首页
pages/about.vue 关于页
pages/404.vue 404页
// src/pages/index.vue <template> <div> 后台首页 </div> </template> // src/pages/about.vue <template> <div> 关于页 </div> </template> // src/pages/404.vue <template> <div> <el-result icon="warning" title="404提示" sub-title="你找的页面走丢了" > <template #extra> <el-button type="primary" @click="$router.push('/')"> 回到首页 </el-button> </template> </el-result> </div> </template>
...略 import Index from '~/pages/index.vue' // + import About from '~/pages/about.vue' // + import NotFound from '~/pages/404.vue' // + // 2.定义一些路由 const routes = [ { path: "/", component: Index, name: "index"}, // + { path: "/about", component: About, name: "about"}, // + { path: '/:pathMatch(.*)*', name: 'NotFound', component: NotFound }, // + 404 Not found 路由 ]
<script setup> </script> <template> <router-view></router-view> // + </template> <style> </style>
Last updated 1 year ago