router传参的多种方式

router传参的多种方式

  1. 方式1
    Login.vue中(需要页面跳转的组件)

this.$router.push("/index?account="+this.account);//方式1

index.vue中(跳转后接收参数的组件)

account:this.$route.query.account,//方式1的获取方式

  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. 结合方式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. 类似方式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的获取方式

  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

  1. 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’],

  1. 通过路由的方式跳转并传参<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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值