The role of props configuration in routing is to make it easier for routing components to receive parameters.It is very convenient to use. The following describes three ways to use props to pass and receive parameters.
Pass arguments of object type
{ name:"ddd",
path:"ddd/:id/:name",
component:DDD,
props:{a:100,b:"hello!!!"}
}
//
export default {
name: 'DDD',
props:["a","b"],
props is set to true, only parameters of type params will be passed
{ name:"ddd",
path:"ddd/:id/:name",
component:DDD,
props:true
}
//
<router-link class="list-a"
:to="{
name:'ddd',
params:{
id:item.id,
name:item.name
}
}"
>{{item.title}}</router-link>
//
<ul>
<li>{{$route.params.id}}</li>
<li>{{$route.params.name}}</li>
</ul>
How to write props function
{ name:"ddd",
path:"ddd/:id/:name",
component:Detail,
props(router){
return{
id:router.id,
title:router.name,
a:200,
b:"hello!!!"
}
}
}
//
<router-link class="list-a"
:to="{
name:'ddd',
params:{
id:item.id,
name:item.name
}
}"
>{{item.title}}</router-link>
//
<ul>
<li>{{$route.params.id}}</li>
<li>{{$route.params.name}}</li>
<li>{{a}}=={{b}}</li>
</ul>
Comments
Post a Comment