vue之路由的基本使用
app.vue
<template> <div> <h1>vue-router 的基本使用</h1> <!-- 声明路由链接 --> <router-link to="/home">首页</router-link> <router-link to="/movie">电影</router-link> <router-link to="/about">关于</router-link> <hr /> <!-- 路由的占位符 --> <router-view></router-view> </div> </template> <script> export default { name: 'MyApp', } </script> <style></style>
router.js
import { createRouter, createWebHashHistory } from 'vue-router'
import Home from './MyHome.vue'
import Movie from './MyMovie.vue'
import About from './MyAbout.vue'
// 创建路由对象
const router = createRouter({
// 指定路由的工作模式
history: createWebHashHistory(),
// 自定义路由高亮的 class 类
linkActiveClass: 'active-router',
// 声明路由的匹配规则
routes: [
{ path: '/', redirect: '/home' },
{ path: '/home', component: Home },
{ path: '/movie', component: Movie },
{ path: '/about', component: About },
],
})
// 导出路由对象
export default router
main.js
import router from './components/07.next/router' const app = createApp(App) // 挂载路由模块 app.use(router) app.mount('#app')
浙公网安备 33010602011771号