Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getCurrentInstance().router.params取不到值 #7853

Closed
igpfpyu opened this issue Oct 16, 2020 · 15 comments
Closed

getCurrentInstance().router.params取不到值 #7853

igpfpyu opened this issue Oct 16, 2020 · 15 comments
Labels
F-react Framework - React P-1 High, patch T-h5 Target - 编译到 H5 V-3 Version - 3.x

Comments

@igpfpyu
Copy link

igpfpyu commented Oct 16, 2020

相关平台

H5

浏览器版本: Chrome next
使用框架: React

复现步骤

getCurrentInstance().router.params取不到值

期望结果

getCurrentInstance().router.params取到上一个页面跳转到本页面带的参数

实际结果

拿到参数

环境信息

process.env.TARO_API_ENV: undefined

  Taro CLI 3.0.13 environment info:
    System:
      OS: macOS 10.15.7
      Shell: 5.7.1 - /bin/zsh
    Binaries:
      Node: 12.18.3 - /usr/local/bin/node
      Yarn: 1.22.4 - /usr/local/bin/yarn
      npm: 6.14.7 - /usr/local/bin/npm
    npmPackages:
      @tarojs/components: 3.0.14 => 3.0.14 
      @tarojs/mini-runner: 3.0.14 => 3.0.14 
      @tarojs/react: 3.0.14 => 3.0.14 
      @tarojs/runtime: 3.0.14 => 3.0.14 
      @tarojs/taro: 3.0.14 => 3.0.14 
      @tarojs/webpack-runner: 3.0.14 => 3.0.14 
      babel-preset-taro: 3.0.14 => 3.0.14 
      eslint-config-taro: 3.0.14 => 3.0.14 
      nerv-devtools: ^1.5.7 => 1.5.7 
      react: ^16.10.0 => 16.13.1 
      taro-ui: ^3.0.0-alpha.3 => 3.0.0-alpha.3 
    npmGlobalPackages:
      typescript: 4.0.3

@taro-bot2 taro-bot2 bot added F-react Framework - React T-h5 Target - 编译到 H5 V-3 Version - 3.x labels Oct 16, 2020
@igpfpyu
Copy link
Author

igpfpyu commented Oct 16, 2020

router.mode="hash"是取不值的,
router.mode="browser"是可以取到值的

@xw-Zhou
Copy link

xw-Zhou commented Oct 16, 2020

router.mode="hash"是取不值的,
router.mode="browser"是可以取到值的

哪里可以设置?

@igpfpyu
Copy link
Author

igpfpyu commented Oct 17, 2020

router.mode="hash"是取不值的,
router.mode="browser"是可以取到值的

哪里可以设置?

config -> index

@ZYK1236
Copy link

ZYK1236 commented Oct 17, 2020

看看传递的参数中有没有包括'?'这个字符

@igpfpyu
Copy link
Author

igpfpyu commented Oct 17, 2020

有的,就是hash模式就跟不到值

@ZYK1236
Copy link

ZYK1236 commented Oct 17, 2020

反正我之前测的是如果参数带有'?'这个字符,那么后面的参数就传不出去

@JakeWoki
Copy link

vue 也用不了getCurrentInstance

@tangzhangzheng
Copy link

tangzhangzheng commented Oct 22, 2020

config-> index->h5 添加代码 router: { mode: 'browser' },将hash路由更改为browser路由,重新编译 如果不更换hash路由模式,请将getCurrentInstance().router.params替换Current.router.params

@Chen-jj Chen-jj added the P-1 High, patch label Oct 23, 2020
@taro-bot2 taro-bot2 bot added this to the 3.0.16 milestone Oct 23, 2020
@xiehui999
Copy link

xiehui999 commented Oct 29, 2020

3.0.14版本也有问题 ,将getCurrentInstance().router.params替换Current.router.params 还是获取不到参数啊。 @Chen-jj @tangzhangzheng .

修改 router: { mode: 'browser' } 是可以正常工作的,还有不加这个配置,路由调整和之前(3.0.5)也不一样了,

1,3.0.5 时H5打开路由是 http://192.168.31.34:10086/subpackages/doctor/pages/DoctorDetail?pId=8
2,升级3.0.14 时 路由变成http://192.168.31.34:10086/#/subpackages/doctor/pages/DoctorDetail?pId=8 (此时上面说的两种方式都获取不到参数)
3, 增加 配置 router: { mode: 'browser' }后,路由和 1一样了,也能获取到参数了。

问一下,为什么会有上面的表现

@tangzhangzheng
Copy link

3.0.14版本也有问题 ,将getCurrentInstance().router.params替换Current.router.params 还是获取不到参数啊。 @Chen-jj @tangzhangzheng .

修改 router: { mode: 'browser' } 是可以正常工作的,还有不加这个配置,路由调整和之前(3.0.5)也不一样了,

1,3.0.5 时H5打开路由是 http://192.168.31.34:10086/subpackages/doctor/pages/DoctorDetail?pId=8
2,升级3.0.14 时 路由变成http://192.168.31.34:10086/#/subpackages/doctor/pages/DoctorDetail?pId=8 (此时上面说的两种方式都获取不到参数)
3, 增加 配置 router: { mode: 'browser' }后,路由和 1一样了,也能获取到参数了。

问一下,为什么会有上面的表现

@xiehui999
1,根据url,路由方式为hash路由模式,
2,根据url,路由方式为browser路由模式,
3,增加 配置 router: { mode: 'browser' },更改为browser路由模式,当然会和1一样了。
(此时上面说的两种方式都获取不到参数,可以确认下是否import Taro, {Current} from '@tarojs/taro'确认引入,不行再可以看下location对象下的各个属性,使用截取字符串的方式获取传过来的值)

hash路由和browser路由模式可以简单看下,
5分钟了解哈希路由https://www.bilibili.com/video/BV15i4y1M7Xn
10分钟学会browser路由 https://www.bilibili.com/video/BV15i4y1M7Xn

@tourze
Copy link
Contributor

tourze commented Nov 1, 2020

这个问题在3.0.15依然存在

@Chen-jj
Copy link
Contributor

Chen-jj commented Nov 3, 2020

@tourze 这个 PR 已经修复了这个问题,3.0.15 应该是无问题的。

Duplicate of #7871

@Chen-jj Chen-jj closed this as completed Nov 3, 2020
@Chen-jj Chen-jj removed this from the 3.0.16 milestone Nov 3, 2020
@Chen-jj
Copy link
Contributor

Chen-jj commented Nov 3, 2020

vue 也用不了getCurrentInstance

不会用不了吧,可以另外提个 issue

@JakeWoki
Copy link

vue 也用不了getCurrentInstance

不会用不了吧,可以另外提个 issue

可以了,文档不够详细,没说要引入import {getCurrentInstance} from "@tarojs/taro"才能用

@Whalesongs
Copy link

感谢 可以了,文档不够详细,没说要引入import {getCurrentInstance} from "@tarojs/taro"才能用

@ZakaryCode ZakaryCode moved this to Done in H5 Apr 10, 2023
@ZakaryCode ZakaryCode added this to H5 Apr 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-react Framework - React P-1 High, patch T-h5 Target - 编译到 H5 V-3 Version - 3.x
Projects
Archived in project
Development

No branches or pull requests

9 participants