在一个单独的js文件中配置映射关系和模式(hash或history),这里是hash模式。
// 导入createRouter函数和hash模式
import { createRouter, createWebHashHistory } from 'vue-router'// 导入要配置映射关系的组件
import Home from './Home.vue'
import About from './About.vue'// 创建路由
const router = createRouter({// 要指定的模式:hash/historyhistory: createWebHashHistory(),// 要配置的映射关系:路径为path时就显示对应componentroutes: [{ path: '/home', component: Home },{path:'/about',component:About}]
})// 将路由导出,供外界使用
export default router
在main.js中导入并注册。
import { createApp } from 'vue'import App from './router/App.vue'
// 导入路由
import router from './router/index'const app= createApp(App)
// 注册路由
app.use(router)
app.mount('#app')
router-link
会显示一个超链接,点了就会让url改,且显示对应组件(如何对应在映射关系里配置了)。
router-view
是对应组件显示的地方。
Appcontent
首页
关于
配置规则rule:
"vue/multi-word-component-names": "off"
Vue全家桶 Vue-router的详细介绍
Component name “About“ should always be multi-word.(vue/multi-word-component-names)