VUE:router路由使用

1.安装

pnpm add vue-router@4

2.在main.js中导入

import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import App from './App.vue'
import router from './router' //1导入路由
const app = createApp(App)

app.use(ElementPlus)
app.use(router) //2.应用到app
app.mount('#app') //挂载

3.创建layout路由布局文件

import { createRouter, createWebHistory } from 'vue-router'

//导入组件
import LoginVue from '@/views/Login.vue'
import LayoutVue from '@/views/Layout.vue'


//定义路由关系
const routes = [
    { path: '/login', component: LoginVue },
    {
        path: '/', component: LayoutVue,redirect:'/article/manage', children: [
           //子路由配置区
        {path: 路径 ,component: 导入的子组件名称}
        ]
    }
]

//创建路由器
const router = createRouter({
    history: createWebHistory(),
    routes: routes
})

//导出路由
export default router

4.在APP.vue中使用路由进行渲染

<script setup>
  
</script>

<template>
  <router-view></router-view> //渲染路由的关键标签
</template>

<style scoped>

</style>

5.成功

相关推荐
张小瑜几秒前
elementUI中el-form 嵌套el-from 如何进行表单校验?
前端·vue.js·elementui
盼盼盼7 分钟前
react Refine 框架在性能优化方面有哪些具体的技术手段?
前端·react.js·性能优化
听潮阁12 分钟前
【卷起来】VUE3.0教程-09-整合Element-plus
前端·javascript·vue.js·spring boot·spring cloud
风清云淡_A14 分钟前
react18基础教程系列-- 框架基础理论知识mvc/jsx/createRoot
前端·reactjs
恋猫de小郭20 分钟前
React Native 0.76,New Architecture 将成为默认模式,全新的 RN 来了
javascript·react native·react.js
让开,我要吃人了27 分钟前
HarmonyOS应用开发( Beta5.0)HOS-用户认证服务:面部识别
服务器·前端·华为·移动开发·嵌入式·harmonyos·鸿蒙
小小李程序员29 分钟前
Redis地理数据类型GEO
前端·redis·bootstrap
黑客大佬35 分钟前
安全工具 | 使用Burp Suite的10个小tips
运维·前端·网络·学习·安全·云计算·腾讯云
fmc1211041 小时前
Excel文档的读取(3)
java·前端·python
生椰拿铁You1 小时前
CSS——盒子模型
前端·css