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

使用Taro.chooseImage 选择图片后,调用Taro.getCurrentInstance获取到的router和page是空的 #16155

Open
ice909 opened this issue Jul 25, 2024 · 6 comments
Labels
F-vue3 Framework - Vue 3 T-weapp Target - 编译到微信小程序 V-3 Version - 3.x

Comments

@ice909
Copy link
Contributor

ice909 commented Jul 25, 2024

相关平台

微信小程序

小程序基础库: 3.4.7
使用框架: Vue 3

复现步骤

使用Taro.chooseImage 选择图片后,调用Taro.getCurrentInstance获取到的router和page是空的
这个是大概的场景,不知道是不是因为小程序选择图片弹出了新的窗口,选择完毕后导致这个问题?
image

期望结果

可以正确获取当前页面

实际结果

获取到的router 和page 都是null

环境信息

👽 Taro v3.6.20


  Taro CLI 3.6.20 environment info:
    System:
      OS: Linux 6.6 Deepin 23 23
      Shell: 5.9 - /usr/bin/zsh
    Binaries:
      Node: 20.14.0 - ~/.config/nvm/versions/node/v20.14.0/bin/node
      npm: 10.7.0 - ~/.config/nvm/versions/node/v20.14.0/bin/npm
    npmPackages:
      @tarojs/cli: 3.6.20 => 3.6.20 
      @tarojs/components: 3.6.20 => 3.6.20 
      @tarojs/helper: 3.6.20 => 3.6.20 
      @tarojs/plugin-framework-vue3: 3.6.20 => 3.6.20 
      @tarojs/plugin-html: 3.6.20 => 3.6.20 
      @tarojs/plugin-platform-alipay: 3.6.20 => 3.6.20 
      @tarojs/plugin-platform-h5: 3.6.20 => 3.6.20 
      @tarojs/plugin-platform-jd: 3.6.20 => 3.6.20 
      @tarojs/plugin-platform-qq: 3.6.20 => 3.6.20 
      @tarojs/plugin-platform-swan: 3.6.20 => 3.6.20 
      @tarojs/plugin-platform-tt: 3.6.20 => 3.6.20 
      @tarojs/plugin-platform-weapp: 3.6.20 => 3.6.20 
      @tarojs/runtime: 3.6.20 => 3.6.20 
      @tarojs/shared: 3.6.20 => 3.6.20 
      @tarojs/taro: 3.6.20 => 3.6.20 
      @tarojs/taro-loader: 3.6.20 => 3.6.20 
      @tarojs/webpack5-runner: 3.6.20 => 3.6.20 
      babel-preset-taro: 3.6.20 => 3.6.20 
      eslint-config-taro: 3.6.20 => 3.6.20 


@taro-bot2 taro-bot2 bot added F-vue3 Framework - Vue 3 T-weapp Target - 编译到微信小程序 V-3 Version - 3.x labels Jul 25, 2024
This was referenced Jul 27, 2024
@ice909
Copy link
Contributor Author

ice909 commented Aug 7, 2024

@TheKonka 这个能复现吗?
https://github.com/linuxdeepin/bbs-mobile-web
也是这个项目的

@ct-0902
Copy link

ct-0902 commented Aug 21, 2024

调用扫码相册返回也是这种情况,在有些ios机型下就是必现的

@ice909
Copy link
Contributor Author

ice909 commented Aug 22, 2024

调用扫码相册返回也是这种情况,在有些ios机型下就是必现的

我这是在安卓下,也是必现的

@adjfks
Copy link

adjfks commented Sep 14, 2024

确实有这个问题

@ice909
Copy link
Contributor Author

ice909 commented Sep 20, 2024

@TheKonka 能看下这个吗?

@zhang0ZGC
Copy link

zhang0ZGC commented Nov 21, 2024

应该是选择图片的时候触发了 onHide 生命周期了, onHide 时内部会把 Current.page Current.router 置为 null,onShow 时又会把 page 跟 router 设置回来

估计选择完图片后就立即执行回调了,这时候还没触发 onShow,建议在选择图片前把 router 缓存一下

onShow () {
Current.page = this as any
this.config = pageConfig || {}
const path = getPath(id, this.options)
const router = isBrowser ? path : this.route || this.__route__
Current.router = {
params: this.options,
path: addLeadingSlash(router),
onReady: getOnReadyEventKey(id),
onShow: getOnShowEventKey(id),
onHide: getOnHideEventKey(id)
}
raf(() => {
eventCenter.trigger(getOnShowEventKey(id))
})
safeExecute(path, 'onShow')
},

onHide () {
Current.page = null
Current.router = null
const path = getPath(id, this.options)
safeExecute(path, 'onHide')
eventCenter.trigger(getOnHideEventKey(id))
},

不过这样也确实很坑,在 render 方法里获取路由参数这些也要尤其注意,在此场景下render方法里使用 this.$instance.router.params 对象会因为报 router 为空对象而报错,兜兜转转还是得在类组件初始化时就定义一个 $router 对象把路由缓存下来

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-vue3 Framework - Vue 3 T-weapp Target - 编译到微信小程序 V-3 Version - 3.x
Projects
Status: Todo
Development

No branches or pull requests

4 participants