一、目录结构
二、安装router
2.1 执行命令
npm install vue-router
2.2 安装完成
2.3 进入项目,创建router目录,再去创建index.js
三、配置src/router/index.js文件内容
import { createRouter, createWebHistory } from "vue-router";
import Home from "../views/Home.vue";
const routes = [
{
path: "/",
name: "Home",
component: Home,
},
{
path: "/about",
name: "About",
component: () =>
import(/* webpackChunkName: "about" */ "../views/About.vue"),
},
];
const router = createRouter({
history: createWebHistory(),
routes,
});
export default router;
四、在src目录新建views/Home.vue和About.vue
五、App.vue文件内容修改
5.1 原本为:
5.2 修改成:
<template>
<router-view></router-view>
</template>
六、main.js挂载使用router
import { createApp } from 'vue'
import App from './App.vue'
import router from "./router";
createApp(App).use(router).mount('#app')