import { createRouter, createWebHistory } from 'vue-router'
createRouter:用来创建 路由
createWebHistory :url的格式 不带#。
// 导入页面 import a from '../components/a.vue' import b from '../components/b.vue' import c from '../components/c.vue'
const routes=[ { path: '/a', name: 'a', component: a }, { path: '/b', name: 'b', component: b }, { path: '/c', name: 'c', component:c },{ path:'/', redirect:'/a' }]
const routers=createRouter({ routes, history:createWebHistory() }) export default routers
导入 封装的router.js页面,然后直接在vue里面使用
import routers from './router/router.js' var app=createApp(App) app.use(routers)
ps:默认没有 var app=createApp(App),只有createApp(App).mount('#app')。把createApp(App)提出来就好。
<router-link to="/a">a</router-link> <router-link to="/b">b</router-link> <router-link to="/c">c</router-link> <router-view></router-view>