1. 安装vue-router
npm install vue-router@4
2. 在src目录下新建router文件夹及index.ts文件
3. index.ts文件中声明路由文件
import { RouteRecordRaw, createRouter, createWebHistory } from 'vue-router';
const routes: Array<RouteRecordRaw> = [
{
path: '/',
component: () => import('@/views/Index.vue')
}
]
const router = createRouter({
history: createWebHistory(),
routes
})
export default router
4. 再main.ts文件中引入路由文件并挂载
import { createApp } from 'vue'
import 'normalize.css'
import App from './App.vue'
import router from './router'
const app = createApp(App)
app.use(router)
app.mount('#app')