安装:npm i vue-router@next -S 安装vue3下的路由
1.创建一个路由文件 router.js
2.导入路由模块
import {createRouter,createWebHashHistory} from 'vue-router'
3.引入要链接的组件
import test01 from './components/test01.vue'
4.创建路由路径
const routes = [
{
path: '/', //跳转的路径
name: 'test01', //路径的名字
component:test01, //组件名字
},
{path: '/test02', name: 'test02', component:test02,},
{path: '/test03', name: 'test03', component:test03, }
]
5.创建路由对象
const router = createRouter({
history: createWebHashHistory(process.env.BASE_URL),
routes
})
6.把对象导出给外部使用
export default router
createWebHashHistory -> hash模式
createWebHistory -> history模式
菜单1 定义菜单
显示路由里的内容
属性:
to:跳转路径
replace 跳转路径,导航后不会留下history历史记录
tag 替换 router-link 显示的标签
active-class 设置菜单被选中的样式
const routes = [
// {path:'/', redirect:"test02"}, //默认路径
{
path: '/test01', //跳转的路径
name: 'test01', //路径的名字
component:test01, //组件名字
},
{path: '/test02', name: 'test02', component:test02, },
{path: '/test03', name: 'test03', component:test03, },
{path: '/test04', name: 'test04', component:test04, },
{path: '/test05', name: 'test05', component:test05, },
{path: '/test06', name: 'test06', component:test06, }
]
this.$router.push({path:"/test01", query:{name:"ton"}})
this.$router.push({path:"/test01", params:{name:"ton"}})
this.$router.push({name:"test-01", params:{name:"ton"}})
[this.$route.query.name](http://this.$route.query.name)
[this.$route.params.name](http://this.$route.params.name)