router传参的多种方式
- 方式1
Login.vue中(需要页面跳转的组件)
this.$router.push("/index?account="+this.account);//方式1
index.vue中(跳转后接收参数的组件)
account:this.$route.query.account,//方式1的获取方式
- 方式2
Login.vue中(需要页面跳转的组件)
this.$router.push("/index/1952683834);//方式2
router/index.js中(路由的路径需要配置)
path: '/index/:qqId ’
index.vue中(跳转后接收参数的组件)
qqId:this.$route.params.qqId//方式2的获取方式
- 结合方式1和方式2
Login.vue中(需要页面跳转的组件)
this.$router.push("/index/1952683834?account="+this.account);//结合方式1和方式2
router/index.js中(路由的路径需要配置)
path: '/index/:qqId ’
index.vue中(跳转后接收参数的组件)(接收两个参数)
account:this.$route.query.account,//方式1的获取方式
qqId:this.$route.params.qqId//方式2的获取方式
更常用的方式:分开写(4-7)
4. 类似方式1(使用path和query)
Login.vue中(需要页面跳转的组件)
this.$router.push({path:"/index",query:{account:this.account}});//使用path和query
index.vue中(跳转后接收参数的组件)
account:this.$route.query.account,//方式1的获取方式
- 类似方式1(使用name和query)(name较path更方便)
Login.vue中(需要页面跳转的组件)
this.$router.push({name:“index”,query:{account:this.account}});//使用name和query(name需要在路由中命名)
router/index.js中(路由中添加name)
name: ‘index’,
index.vue中(跳转后接收参数的组件)
account:this.$route.query.account,//方式1的获取方式
- 类似方式2(使用name和params)
Login.vue中(需要页面跳转的组件)
this.$router.push({name:“index”,params:{account:this.account}});//使用name和params(name需要在路由中命名,params需要在路由的路径中写上)
router/index.js中(路由的路径需要配置)
path: '/index/:account ’
index.vue中(跳转后接收参数的组件)
account:this.$route.params.account
- Props方式-支持路由传参(结合方式6)(只能对params的方式使用)
Login.vue中(需要页面跳转的组件)
this.$router.push({name:“index”,params:{account:this.account}});//使用name和params(name需要在路由中命名,params需要在路由的路径中写上)
router/index.js中(路由的路径需要配置;props支持传参添加上)
path: '/index/:account ',
props: true,
index.vue中(跳转后接收参数的组件)(直接添加props,无需在数据处添加)
props:[‘account’],
- 通过路由的方式跳转并传参
<router-link to=”/url”></router-link>
(该方式使用<router-view/>
显示所对应的路由组件)
Left.vue中(需要页面跳转的组件)
<!-- 通过路由的方式跳转 -->
<!--:to相当于v-bind:to的简化写法-->
<router-link :to="{name:'taobao',params:{href:'http://www.taobao.com'}}">taobao</router-link><br/>
<router-link :to="{name:'baidu',params:{href:'http://www.baidu.com'}}">baidu</router-link>
router/index.js中(添加name和props支持传参)
{
path: ‘/taobao:href’,
name: ‘taobao’,
props: true,
component: Taobao
},
{
path: ‘/baidu:href’,
name: ‘baidu’,
props: true,
component: Baidu
}
Taobao.vue(baidu.vue)中(跳转后获取参数的组件)(直接添加props,无需在数据处添加)
props:[“href”],
填充参数:
<a :href="href">淘宝</a>