在 router 文件夹下新建 index.js
import { createRouter, createWebHistory } from "vue-router";
import baseRouter from "./baseRouter";
// 构建路由 函数
const _createRouter = () =>
createRouter({
history: createWebHistory(),
routes: baseRouter,
scrollBehavior() {
return {
el: "#app",
top: 0,
behavior: "smooth",
};
},
});
// 重置路由 函数
export const resetRouter = () => {
const newRouter = _createRouter();
router.matcher = newRouter.matcher;
};
const router = _createRouter();
export default router;
在 router 文件夹下新建 baseRouter.js
export default [
{
path: "/login",
name: "login",
component: () => import("@/views/login.vue"),
},
];
在 views 文件下新建 login.vue
<script setup></script>
<template>
<div class="login">login</div>
</template>
<style lang="scss" scoped></style>
在 main.js 中使用 router
import { createApp } from "vue";
import App from "./App.vue";
import router from "./router/index.js";
const app = createApp(App);
app.use(router);
app.mount("#app");