《nodejs跨栏》vue篇——路由

发布时间 2023-05-09 16:50:25作者: Fusio

路由是什么

参考链接:https://www.cnblogs.com/chengqiang521/p/15512719.html

router简介

路由,一个router就是一组key-value对,只不过这里的key是路径,就是path,value是组件。

基本使用

1安装vue-router

npm i vue-router

2应用插件

Vue.use(VueRouter)

3编写router配置项

点击查看代码
//引入VueRouter
import VueRouter from 'vue-router'
//引入Luyou 组件
import About from '../components/About'
import Home from '../components/Home'

//创建router实例对象,去管理一组一组的路由规则
const router = new VueRouter({
	routes:[
		{
			path:'/about',
			component:About
		},
		{
			path:'/home',
			component:Home
		}
	]
})

//暴露router
export default router

4实现切换(active-class可配置高亮样式)

<router-link active-class="active" to="/about">About</router-link>

5指定展示位置

<router-view></router-view>

多级路由

1配置路由规则,使用children配置项:

点击查看代码
routes:[
	{
		path:'/about',
		component:About,
	},
	{
		path:'/home',
		component:Home,
		children:[ //通过children配置子级路由
			{
				path:'news', //此处一定不要写:/news
				component:News
			},
			{
				path:'message',//此处一定不要写:/message
				component:Message
			}
		]
	}
]

2跳转写完整路径

<router-link to="/home/news">News</router-link>

路由的query参数

1传递参数

点击查看代码
<!-- 跳转并携带query参数,to的字符串写法 -->
<router-link :to="/home/message/detail?id=666&title=你好">跳转</router-link>
<!-- 跳转并携带query参数,to的对象写法 -->
<router-link
	:to="{
		path:'/home/message/detail',
		query:{
		   id:666,
            title:'你好'
		}
	}"
>跳转</router-link>

2接收参数:

$route.query.id
$route.query.title

命名路由

可以简化路由的跳转。

1给路由命名:

点击查看代码
{
	path:'/demo',
	component:Demo,
	children:[
		{
			path:'test',
			component:Test,
			children:[
				{
                      name:'hello' //给路由命名
					path:'welcome',
					component:Hello,
				}
			]
		}
	]
}
2简化跳转:
点击查看代码
<!--简化前,需要写完整的路径 -->
<router-link to="/demo/test/welcome">跳转</router-link>

<!--简化后,直接通过名字跳转 -->
<router-link :to="{name:'hello'}">跳转</router-link>

<!--简化写法配合传递参数 -->
<router-link 
	:to="{
		name:'hello',
		query:{
		   id:666,
            title:'你好'
		}
	}"
>跳转</router-link>

路由的params参数

1配置路由,声明接收params参数

点击查看代码
{
	path:'/home',
	component:Home,
	children:[
		{
			path:'news',
			component:News
		},
		{
			component:Message,
			children:[
				{
					name:'xiangqing',
					path:'detail/:id/:title', //使用占位符声明接收params参数
					component:Detail
				}
			]
		}
	]
}

2传递参数(特别注意:路由携带params参数时,若使用to的对象写法,则不能使用path配置项,必须使用name配置!)

点击查看代码
<!-- 跳转并携带params参数,to的字符串写法 -->
<router-link :to="/home/message/detail/666/你好">跳转</router-link>
<!-- 跳转并携带params参数,to的对象写法 -->
<router-link
	:to="{
		name:'xiangqing',
		params:{
		   id:666,
            title:'你好'
		}
	}"
>跳转</router-link>

3接收参数:

$route.params.id
$route.params.title

路由的props配置

作用:让路由组件更方便的收到参数

点击查看代码
{
	name:'xiangqing',
	path:'detail/:id',
	component:Detail,

	//第一种写法:props值为对象,该对象中所有的key-value的组合最终都会通过props传给Detail组件
	// props:{a:900}

	//第二种写法:props值为布尔值,布尔值为true,则把路由收到的所有params参数通过props传给Detail组件
	// props:true
	//第三种写法:props值为函数,该函数返回的对象中每一组key-value都会通过props传给Detail组件
	props(route){
		return {
			id:route.query.id,
			title:route.query.title
		}
	}
}