传递参数有两种类型:
-
params和query
params类型
- 配置路由格式:
/router/:id
- 传递的方式:在path后面跟上对应的值
- 传递后形成的路径:
/router/123
,/router/abc
①路由配置格式,使用冒号':'
{ path: '/user/:user_id', component: User}
②为新添加的url动态赋值
<router-link :to="'/user/'+user_id">User</router-link>
data(){
return {
user_id: 'xml'
}
}
③调用当前路由对象的params即可获取参数信息
<h2>{{$route.params}}</h2>
{ "user_id": "xml" }
query类型
- 配置路由格式:/router,也就是普通配置
- 传递的方式:对象中使用query的键值对作为传递方式
- 传递后形成的路径:
/router?id=123
,/router/?id=abc
//仍是普通配置方式
{
path: '/profile',
component: Profile
}
<!-- 在跳转路径中传入query参数信息,
如果觉得传入的参数过多,同样可以将其抽取出来-->
<router-link :to="{path:'/profile', query:{name:'AG',age:18,height:1.68}}">Profile</router-link>
<!-- 拿到的这个query对象为:{ "name": "AG", "age": "18", "height": "1.68" } -->
<h2>{{$route.query}}</h2>