taro Current.router.params获取的中文是乱码

inb24sb2  于 2个月前  发布在  其他
关注(0)|答案(5)|浏览(36)

相关平台

H5

复现仓库

https://github.com/SidneyLann/taro-test-repo.git
浏览器版本: 谷歌最新版
使用框架: React

复现步骤

  1. npm run dev:h5
  2. 点击按钮
  3. 在跳转到的页面里,中文显示是未解析的%E4%B8%AD%E6%96%87

期望结果

通过Current.router.params接收的中文内容能正常显示.

实际结果

未解析的中文编码

环境信息

👽 Taro v3.6.7

  Taro CLI 3.6.7 environment info:
    System:
      OS: Windows 10 10.0.22621
    Binaries:
      Node: 18.14.0 - D:\DEV\nodejs\node.EXE
      Yarn: 1.22.19 - D:\DEV\nodejs\yarn.CMD
      npm: 9.3.1 - D:\DEV\nodejs\npm.CMD
    npmPackages:
      @tarojs/cli: 3.6.7 => 3.6.7
      @tarojs/components: 3.6.7 => 3.6.7
      @tarojs/helper: 3.6.7 => 3.6.7
      @tarojs/plugin-framework-react: 3.6.7 => 3.6.7
      @tarojs/plugin-platform-alipay: 3.6.7 => 3.6.7
      @tarojs/plugin-platform-h5: 3.6.7 => 3.6.7
      @tarojs/plugin-platform-jd: 3.6.7 => 3.6.7
      @tarojs/plugin-platform-qq: 3.6.7 => 3.6.7
      @tarojs/plugin-platform-swan: 3.6.7 => 3.6.7
      @tarojs/plugin-platform-tt: 3.6.7 => 3.6.7
      @tarojs/plugin-platform-weapp: 3.6.7 => 3.6.7
      @tarojs/react: 3.6.7 => 3.6.7
      @tarojs/rn-runner: 3.6.7 => 3.6.7
      @tarojs/rn-supporter: 3.6.7 => 3.6.7
      @tarojs/runtime: 3.6.7 => 3.6.7
      @tarojs/runtime-rn: 3.6.7 => 3.6.7
      @tarojs/shared: 3.6.7 => 3.6.7
      @tarojs/taro: 3.6.7 => 3.6.7
      @tarojs/taro-rn: 3.6.7 => 3.6.7
      @tarojs/webpack5-runner: 3.6.7 => 3.6.7
      babel-preset-taro: 3.6.7 => 3.6.7
      eslint-config-taro: 3.6.7 => 3.6.7
      expo: ~47.0.3 => 47.0.13
      react: ^18.1.0 => 18.2.0
      react-native: ^0.70.1 => 0.70.7
cig3rfwq

cig3rfwq1#

调用一下 decodeURIComponent 方法就行了

c86crjj0

c86crjj02#

到处调用嘛? 为什么不是框架调用呢?

5n0oy7gb

5n0oy7gb3#

我自己项目也思考过这个问题,小程序URL参数默认不编码解码,web的会有一次编码解码,我把Taro.navigateTo redirectTo等 都二次封装了一下以使表现一致

w8biq8rn

w8biq8rn4#

框架既然编码了就应该负责解码.

hs1rzwqc

hs1rzwqc5#

taro/packages/taro-router/src/router/page.ts

Lines 102 to 113 in 7619716

| | getsearch(){ |
| | letsearch='?' |
| | if(this.routerMode==='hash'){ |
| | constidx=location.hash.indexOf('?') |
| | if(idx>-1){ |
| | search=location.hash.slice(idx) |
| | } |
| | }else{ |
| | search=location.search |
| | } |
| | returnsearch.substring(1) |
| | } |

不能说是框架编码的, 框架只是帮你从原生接口获取参数

就拿 react-router 来说
Current.router.params 对应的就是useParams
你需求的应该是useSearchParams

相关问题