vue路由学习-向路由组件传递数据

通常有两种写法:param与query

1. 定义路由组件(使用占位写法)

配置路由,设置参数(param/query)

children: [
            {
              path: '/home/solo/msgdetail/:id',
              component: MsgDetail
            }
          ]
':id’表示是params写法

router-link写法(es6) 使用写法

<router-link :to="`/home/solo/msgdetail/${solo.id}`">{{solo.name}}</router-link>

2. 根据id查询其他参数值

id路径

用devtools查询的id路径(请求参数)
const id = this.$route.params.id*1
*1是转换为数字

补充:mounted()在组件使用中值运行一次,可以利用watch去监视路由器的变化

mount()

mounted() {
      setTimeout(() => {
         const allMsgDetail = [
            {
              id: 1,
              name: 'Taeyoon',
              birthday: '1989/3/9'  
            },
            {
              id: 3,
              name: 'Somi',
              birthday: '2001/3/9'  
            },
            {
              id: 5,
              name: 'Jennie',
              birthday: '1996/1/16'  
            },
            {
              id: 9,
              name: 'hyuna',
              birthday: '1992/6/6'  
            }
          ]
        this.allMsgDetail = allMsgDetail
        const id = this.$route.params.id*1
        this.msgDetail = allMsgDetail.find(detail => detail.id===id)
      },1000)
    },

watch

watch: {
      $route: function(value){ //
        const id = value.params.id*1
        this.msgDetail = this.allMsgDetail.find(detail => detail.id===id)
      }
    },

补充:利用属性携带数据

<router-view :msg="msg"></router-view>
<router-view msg="msg"></router-view>
:表示msg是变量,无:msg表示字符串
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值