Vue3 中使用 vue-router
- 首先安装 vue-router
cnpm i vue-router@next
- 在 router 文件夹下新建 index.js
import { createRouter, createWebHashHistory, createWebHistory } from "vue-router"
import index from "../components/index.vue"
import ywcz from "../components/ywcz.vue"
const routes = [
{
path: "/",
redirect: "/index",
},
{ path: "/index", component: index },
{ path: "/ywcz", component: ywcz },
]
const router = createRouter({
history: createWebHashHistory(),
routes,
})
export default router
- 在 main.js 引入 router
import router from "./router"
createApp(App).use(router).mount("#app")
- 在 App.vue 使用
<div>
<router-view></router-view>
</div>