Taro: Taro路由传参无法使用this.$router.params获取参数

Created on 11 Oct 2020  ·  13Comments  ·  Source: NervJS/taro


相关平台

H5

复现仓库

https://github.com/weijunfu/app-donation/tree/main/src/pages/publish
浏览器版本: Chrome 84.0.4147.105(正式版本) (64 位)
使用框架: Nerv

复现步骤

  1. 在 pages/publish/index页面,使用Taro.navigateTo跳转至pages/publish/next/index?id=1&name=wei
  2. 在pages/publish/next/index页面中componentWillMount方法中,使用console.log(this.$router.params),报错TypeError: Cannot read property 'params' of undefined;使用console.log(this.$router),打印undefined;

期望结果

在pages/publish/next/index页面中componentWillMount方法中,使用console.log(this.$router.params),直接打印:{id:1, name='wei'}

实际结果

TypeError: Cannot read property 'params' of undefined

环境信息

开发工具:vs code 1.50.0

Taro CLI 3.0.11 environment info:
    System:
      OS: Windows 10
    Binaries:
      Node: 12.18.4
      Yarn: 1.22.4
      npm: 6.14.5

F-nerv T-h5 V-3

Most helpful comment

注意: 第一点:首先,引入 “getCurrentInstance”,如下:
import Taro, {getCurrentInstance} from '@tarojs/taro'

第二点:使用
let { id } = getCurrentInstance().router.params

这样就可以获取值了

All 13 comments

我也遇见了,3.0.14版本,用了所有方法都拿不到路由参数,只能自己根据Taro_ENV===h5来自己写js获取路径参数

通过getCurrentInstance获取路由参数

通过getCurrentInstance获取路由参数

Taro.getCurrentInstance() 用这个也不行的啊

我也遇见了,3.0.14版本,用了所有方法都拿不到路由参数,只能自己根据Taro_ENV===h5来自己写js获取路径参数

这岂不是很麻烦

通过getCurrentInstance获取路由参数

Taro.getCurrentInstance() 用这个也不行的啊

看文档:https://taro-docs.jd.com/taro/docs/react
image

通过getCurrentInstance获取路由参数

Taro.getCurrentInstance() 用这个也不行的啊

看文档:https://taro-docs.jd.com/taro/docs/react
image

你先自己升级到3版本试一下啊,照着文档来没用

通过getCurrentInstance获取路由参数

Taro.getCurrentInstance() 用这个也不行的啊

看文档:https://taro-docs.jd.com/taro/docs/react
image

你先自己升级到3版本试一下啊,照着文档来没用

我是3.0.9啊,另外个项目是3.1.0-alpha,都是这么取的

通过getCurrentInstance获取路由参数

Taro.getCurrentInstance() 用这个也不行的啊

看文档:https://taro-docs.jd.com/taro/docs/react
image

你先自己升级到3版本试一下啊,照着文档来没用

我是3.0.9啊

那你升到3.0.14试一下,Currnt,getCurrentInstance,useRouter都获取不到,我们说的是h5环境下

感谢各位,我把Taro降级到v3.0.9 就可以了

官网有对应解释,我的解决方案:https://blog.csdn.net/weixin_44523860/article/details/109402211

注意: 第一点:首先,引入 “getCurrentInstance”,如下:
import Taro, {getCurrentInstance} from '@tarojs/taro'

第二点:使用
let { id } = getCurrentInstance().router.params

这样就可以获取值了

服气。。。竟然还得降级才能拿到参数

服气。。。竟然还得降级才能拿到参数

不一定要降级,_要多看文档_ 。按照下面的,就能获取值

第一点:首先,引入 “getCurrentInstance”,如下:
import Taro, {getCurrentInstance} from '@tarojs/taro'

第二点:使用
let { id } = getCurrentInstance().router.params

这样就可以获取值了

Was this page helpful?
0 / 5 - 0 ratings