赞
踩
//引入:createRouter(Router);createWebHistory(history模式);createWebHashHistory(hash模式)
import { createRouter, createWebHistory } from 'vue-router'
//routes是一组路由,把里面每一条路由组合起来,形成一个数组
const routes = [
//注册一个路由
{
//相当于给path取个别名,方便使用,不写默认为default,路由跳转时可以使用
name: 'About',
//meta:头部信息;title:标签页名称;activeMenu:激活菜单的path
meta: {title: '首页', activeMenu: '/result/result_survey'},
//自定义访问的url,不要包含或等于 vue.config.js 中的proxy
//path不能重复,否则[Vue warn]: Duplicate keys detected: '/'. This may cause anupdate error
path: '/about',
/**
* 该url指向的组件
* 引入组件两种方式
* 1、import:webpack打包时路由里的所有component都会打包在一个js中,页面多的话,这个文件会很大,造成进入首页时,需要加载的内容过多,时间相对比较长
* import screen from "@/components/screen"
* component: screen
*
* 2、路由懒加载,两种写法,会将component分别打包成不同的js,加载的时候也是按需加载,只用访问这个路由网址时才会加载这个js
* component: (resolve) => require(['@/components/screen'], resolve)
* component: () => import('@/views/Home.vue')
*/
component: () => import('@/views/About.vue')
},
//同一个目录下多个菜单
{
path: '/qte',
component: Layout,
hidden: true,
children: [
{
//localhost:8080/system/dept
name: 'Dept',
meta: {title:'部门列表'},
path: 'dept',
component: (resolve) => require(['@/views/system/dept/index'], resolve)
},
{
//localhost:8080/system/user
name: 'User',
path: 'user',
meta: {title:'用户列表'},
component: (resolve) => require(['@/views/system/user/index'], resolve)
}
]
}
]
const router = createRouter({
//history模式
history: createWebHistory(process.env.BASE_URL),
routes
})
export default router
//引入router
import router from './router'
//router实例注入到vue根实例
createApp(App)
.use(router)
.mount('#app')
<template>
<router-view/>
</template>
//引入路由
import { useRouter } from "vue-router"
export default {
name: "Screen",
setup(){
//定义router对象
const router=useRouter()
//router对象的属性
{
addRoute:
afterEach:
//返回上一个页面
back:() => go(-1)
beforeEach:
beforeResolve:
//当前的路由信息
currentRoute:{
value:{
//path
fullPath: "/screen?id=1",
hash: "",
href: "/screen?id=1",
matched: [{…}]
//meta
meta: {title: '大屏'}
//name
name: "Screen"
//params
params: {}
//path
path: "/screen"
//query
query: {id: '1'}
redirectedFrom: undefined
}
}
//下一个页面
forward:() => go(1)
getRoutes:
go:
hasRoute:
install:
isReady:
listening:
onError:
options:
push:
removeRoute:
replace:
resolve:
}
/**
* 1、query
* url:http://localhost:8081/screen?id=1
*/
//传参
router.push({name:'Screen',query: {id:1}})
//接收
const id=router.currentRoute.value.query.id
/**
* 2、params
* 路由配置的path:path: '/screen/:id/:name'
* url:http://localhost:8081/screen/1/kimi
*
*/
//传参
router.push({name:'Screen',params: {id:1,name:'kimi'}})
//接收
const name=router.currentRoute.value.params.name
//返回上一页面,利用router对象提供的back方法
function jumpBack(){
router.back()
}
return{
id,
name,
jumpBack
}
}
}
<template>
<div class="home">
<router-link :to="{name:'Screen',params: {id:1,name:'kimi'}}">Screen</router-link>
</div>
</template>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。