Vue中路由的props配置
在本篇我会给大家介绍一下关于vue中路由的props配置,在之前我们也了解过组件的props配置的写法就是用来接收外部存进来的东西例如`<student name="???" age="????"/>`这样类型的组件传递数据
方式,现在我则是要说关于路由中的props下面我们拭目以待。

在props中有三种写法分别是 第一种写法:props值为对象,该对象中所有的key-value的组合最终都会通过props传给Detail组件使用`props:{a:900}`;第二种写法:props值为布尔值,布尔值为true,则把路由收到的所有params参数通过props传给Detail组件 `props:true` ;第三种写法:props值为函数,该函数返回的对象中每一组key-value都会通过props传给Detail组件
```js
// 该文件专门用于创建整个应用的路由器
import VueRouter from 'vue-router'
//引入组件
import About from '../pages/About'
import Home from '../pages/Home'
import News from '../pages/News'
import Message from '../pages/Message'
import Detail from '../pages/Detail'
//创建并暴露一个路由器
export default new VueRouter({
    routes:[
        {
            name:'guanyu',
            path:'/about',
            component:About
        },
        {
            path:'/home',
            component:Home,
            children:[
                {
                    path:'news',
                    component:News,
                },
                {
                    path:'message',
                    component:Message,
                    children:[
                        {
                            name:'xiangqing',
                            path:'detail',
                            component:Detail,
                            //props的第一种写法,值为对象,该对象中的所有key-value都会以props的形式传给Detail组件。
                            // props:{a:1,b:'hello'}
                            //props的第二种写法,值为布尔值,若布尔值为真,就会把该路由组件收到的所有params参数,以props的形式传给Detail组件。
                            // props:true
                            //props的第三种写法,值为函数
                            props($route){
                                return {
                                    id:$route.query.id,
                                    title:$route.query.title,
                                    a:1,
                                    b:'hello'
                                }
                            }
                        }
                    ]
                }
            ]
        }
    ]
})
```
### 总结路由的props配置
 作用:让路由组件更方便的收到参数
```js
{
    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
        }
    }
}
```
- 点赞
- 收藏
- 关注作者
 
             
           
评论(0)