Vue项目中使用router

Vite项目中使用router的步骤:

1.安装路由器库:

复制代码
 npm install vue-router@4

2.创建路由配置:通常命名为 router.js 或者 router/index.js

复制代码
 import { createRouter, createWebHashHistory } from 'vue-router';
 import Login from '../views/Login.vue';
 import Index from '../views/Index.vue';
 ​
 const routes = [
     {path:'/',component: Login},
     {
         path:'/index', component: Index
         ,children: [
             {
                 path: '',
                 alias: '/list',
                 component: () => import('../components/User.vue')
             },
             {
                 path: '/upload',
                 component: () => import('../components/Avatar.vue')
             }
         ]
     }   
 ]
 ​
 // 创建路由实例
 const router = createRouter({
     routes,
     history: createWebHashHistory()
 });
 ​
 // 导出路由实例
 export default router;

3.在应用中使用路由:通常是 main.js中使用创建的路由实例

复制代码
 import { createApp } from 'vue'
 import ElementPlus from 'element-plus'
 import 'element-plus/dist/index.css'
 import elementIcon from "./plugins/icons";
 import { createPinia } from 'pinia'
 import axios from "axios"
 // import './style.css'
 import router from './router'
 import App from './App.vue'
 ​
 const pinia = createPinia()
 const app = createApp(App);
 ​
 app.use(ElementPlus);
 app.use(router);
 app.use(pinia);
 app.use(elementIcon);
 app.config.globalProperties.$axios = axios
 ​
 app.mount('#app')

4.在组件中使用路由:在模板中使用 <router-link> 来生成链接

复制代码
 <template>
     <!-- 默认显示的是路由中的 / -->
     <router-view></router-view>
 </template>
相关推荐
Z兽兽1 小时前
React@18+Vite项目配置env文件
前端·react.js·前端框架
SuniaWang2 小时前
《Spring AI + 大模型全栈实战》学习手册系列 · 专题六:《Vue3 前端开发实战:打造企业级 RAG 问答界面》
java·前端·人工智能·spring boot·后端·spring·架构
A_nanda2 小时前
根据AI提示排查vue前端项目
前端·javascript·vue.js
happymaker06263 小时前
web前端学习日记——DAY05(定位、浮动、视频音频播放)
前端·学习·音视频
~无忧花开~3 小时前
React状态管理完全指南
开发语言·前端·javascript·react.js·前端框架
LegendNoTitle3 小时前
计算机三级等级考试 网络技术 选择题考点详细梳理
服务器·前端·经验分享·笔记·php
@大迁世界3 小时前
1.什么是 ReactJS?
前端·javascript·react.js·前端框架·ecmascript
BJ-Giser4 小时前
Cesium 基于EZ-Tree的植被效果
前端·可视化·cesium
王码码20355 小时前
Flutter for OpenHarmony:Flutter 三方库 algoliasearch 毫秒级云端搜索体验(云原生搜索引擎)
android·前端·git·flutter·搜索引擎·云原生·harmonyos
发现一只大呆瓜5 小时前
深入浅出 AST:解密 Vite、Babel编译的底层“黑盒”
前端·面试·vite