taro路由传参正确的接收方式

扯淡

taro路由传参官方文档只说明了怎么传参,但是并没有说明具体怎么去接收参数,它又不像微信小程序那样在onload生命周期里拿到,百度搜出来的解决办法是在componentWillMount生命周期里通过this.$router.params拿到,qunidayede组件里的this里就没有$router属性,打印出来是undefined,这个方法根本行不通。。。下面正文是正确方法

正文

比方说从A页面携带一个参数id跳到B页面

A页面的路由:

Taro.navigateTo({
  url: '/pages/B/B?id='+id
})

B页面接收参数:引入Taro,并在componentWillMount生命周期中通过Taro.getCurrentInstance()获取到路由对象,从中拿到参数

import Taro from '@tarojs/taro'        // 引入Taro
componentWillMount () { 
  let id = Taro.getCurrentInstance().router.params.id
  console.log(id)
}

(完)