1.安装路由
npm i vue-router
2.新建router文件夹在里面新建一个index.js文件
3.刚刚在router中的index.js配置
注意:路由自行配置
import { createRouter, createWebHistory } from 'vue-router'
const routerHistory = createWebHistory()
const router = createRouter({
history: routerHistory,
routes: [
{
path: '/',
component: () => import('../components/HelloWorld.vue')
}, {
path: '/aa',
component: () => import('../view/aa.vue')
}, {
path: '/bb',
component: () => import('../view/bb.vue')
}
]
})
export default router
4.在main.js将路由对象,挂载到 Vue 实例上:
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
createApp(App).use(router).mount('#app')
5.使用App.vue
<template>
<div id="app">
<!-- 导航 -->
<router-link to="/" active-class="current" replace>Home</router-link> |
<router-link to="/aa" active-class="current" replace>aa</router-link> |
<router-link to="/bb">bb</router-link>
<!-- 路由出口 -->
<router-view />
</div>
</template>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>