Vue - 路由组件传参(props配置)
作用:让路由组件更方便的收到参数
布尔模式
props值为布尔值,布尔值为true,则把路由收到的所有params参数通过props传给Info组件
const routes = [
{
path: '/about',
name: 'About',
component: () => import(/* webpackChunkName: "about" */ '../views/About.vue'),
children:[
{
path:'person',
name:'Person',
component: () => import('../views/Person'),
children:[
{
name:'Info',
path:'info',
component: () => import('../views/Info'),
props:true,
}
]
}
]
}
]
对象模式
props值为对象,该对象中所有的key-value的组合最终都会通过props传给Info组件
const routes = [
{
path: '/about',
name: 'About',
component: () => import(/* webpackChunkName: "about" */ '../views/About.vue'),
children:[
{
path:'person',
name:'Person',
component: () => import('../views/Person'),
children:[
{
name:'Info',
path:'info',
component: () => import('../views/Info'),
props:{name:'张三',age:18},
}
]
}
]
}
]
函数模式
props值为函数,该函数返回的对象中每一组key-value都会通过props传给Info组件
const routes = [
{
path: '/',
name: 'Home',
component: Home
},
{
path: '/about',
name: 'About',
// route level code-splitting
// this generates a separate chunk (about.[hash].js) for this route
// which is lazy-loaded when the route is visited.
component: () => import(/* webpackChunkName: "about" */ '../views/About.vue'),
children:[
{
path:'person',
name:'Person',
component: () => import('../views/Person'),
children:[
{
name:'Info',
path:'info',
component: () => import('../views/Info'),
props(route){
return {
name:route.query.name,
age:route.query.age
}
},
}
]
}
]
}
]
接收参数
<template>
<div>
<p>姓名:{{name}}</p>
<p>年龄:{{age}}</p>
</div>
</template>
<script>
export default {
name: "Info",
props:['name','age']
}
</script>