1.安装vue-router
npm install vue-router
2.新建页面 views => home => home.vue
<script setup lang="ts">
</script>
<template>
<div>首页</div>
</template>
<style scoped>
</style>
2.配置路由 router => index.js
import { createRouter, createWebHistory } from "vue-router";
const routes = [
{
path:'/',
name:'Home',
component:()=> import('../views/home/home.vue')
}
]
const router = createRouter({
history:createWebHistory(),
routes
})
export default router
4.在main.ts 引入 router
import { createApp } from 'vue'
import App from './App.vue'
import router from './router/index';
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
const app = createApp(App)
app.use(ElementPlus)
app.use(router)
app.mount('#app')
- 挂载
<template>
<!-- router-view : 路由的整个出口, 用来显示和 URL 对应的组件; -->
<router-view v-slot="{ Component }">
<!-- keep-alive 是Vue提供的一个抽象组件,主要用于保留组件状态或避免重新渲染。 -->
<keep-alive>
<component :is="Component" />
</keep-alive>
</router-view>
</template>
<script>
export default {
}
</script>
<style lang="scss" scoped>
</style>