vue的路由传参

1.使用router-link进行页面按钮跳转

路由文件配置

export default new Router({
  routes: [
    {
      path: '/',
      name: 'homePage',
      component: resolve => require(['../components/home/homePage'], resolve),
    },
    {
      path: '/one/:a',//接受参数
      name: 'one',
      component: resolve => require(['../components/children/one'], resolve),
    }
  ]
})

homePage页面

<template>
  <div>
      homePage页面跳转到one页面
      <router-link to="/one/111">to-one</router-link>
  </div>
</template>

one页面

<template>
  <div>
      我是one页面 {{a}}
  </div>
</template>

<script>
export default {
  name:'one',
  data(){
    return{
      a:0
    }
  },
  mounted(){
    this.a = this.$route.params.a
  }
}
</script>


2.this.$router.push进行编程式路由跳转

homePage页面

<template>
  <div>
      homePage页面跳转到one页面
    <button @click="toOne(222)">to-one</button>
  </div>
</template>
<script>
export default {
    name: 'homePage',
    methods:{
        toOne(a){
            this.$router.push({
                path:`/one/${a}`
            })
        }
    }
}
</script>

one页面和路由配置页面不改变


3.使用path匹配路由,通过query传递参数, query传递的参数会显示在url后面?a=?

homePage页面

<template>
  <div>
      homePage页面跳转到one页面
    <button @click="toOne">to-one</button>
  </div>
</template>
<script>
export default {
    name: 'homePage',
    methods:{
        toOne(){
            this.$router.push({
                path:'/one',
                query:{
                    a:333
                }
            })
        }
    }
}
</script>

one页面

<template>
  <div>
      我是one页面 {{a}}
  </div>
</template>

<script>
export default {
  name:'one',
  data(){
    return{
      a:0
    }
  },
  mounted(){
    this.a = this.$route.query.a
  }
}
</script>

router配置

export default new Router({
  routes: [
    {
      path: '/',
      name: 'homePage',
      component: resolve => require(['../components/home/homePage'], resolve),
    },
    {
      path: '/one',
      name: 'one',
      component: resolve => require(['../components/children/one'], resolve),
    }
  ]
})


4.通过路由属性中的name来匹配的路由,通过params来传递参数

homePage页面

<template>
  <div>
      homePage页面跳转到one页面
    <button @click="toOne">to-one</button>
  </div>
</template>
<script>
export default {
    name: 'homePage',
    methods:{
        toOne(){
            this.$router.push({
                name:'one',
                params:{
                    a:444
                }
            })
        }
    }
}
</script>

one页面

<template>
  <div>
      我是one页面 {{a}}
  </div>
</template>

<script>
export default {
  name:'one',
  data(){
    return{
      a:0
    }
  },
  mounted(){
    this.a = this.$route.params.a
  }
}
</script>

router配置不修改

 

总结:常用路由传参

path配合query传递参数,使用query接收(传递不敏感信息,因为参数会通过?拼接暴露在地址栏)

name配合params传递参数,使用params接收

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值