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

请给小程序也加上路由变更的事件通知 #7903

Closed
anhulife opened this issue Oct 22, 2020 · 4 comments
Closed

请给小程序也加上路由变更的事件通知 #7903

anhulife opened this issue Oct 22, 2020 · 4 comments
Labels
enhancement New feature or request

Comments

@anhulife
Copy link

这个特性解决了什么问题?

小程序目前没法得知路由的变化事件

这个 API 长什么样?

eventCenter.on('__taroRouterChange', onRouteChange);

@taro-bot2 taro-bot2 bot added the enhancement New feature or request label Oct 22, 2020
@rottenpen
Copy link
Contributor

这个需求我前阵子做统计pv的时候也遇到过,因为taro把wx生命周期覆盖了,只能是劫持wx.navigateTo之类关于路由变化的方法实现。不知道官方有没想实现这种需求的计划,我可以贡献一下代码。

@pecopeco
Copy link

pecopeco commented Nov 3, 2020

可以使用wx.onAppRoute做一个监听

@lifegit
Copy link

lifegit commented Jan 23, 2021

其实这个换个思路就好啦:

需求:父(index)向子(detailed)跳转。detailed 加载完毕后收到 index 发来的数据。

正常思路:
index 在路由跳转完毕的回调中 event.trigger 数据。detailed 在 didShow 中监听一个数据。但是现在 navigateTo 的 success 方法是代码执行完毕的方法,不是界面跳转完毕的方法,所以该方案没法正常运行。

现在思路(反过来):

  1. index 在路由跳转前开始监听一个消息 。
  2. 在 detailed 跳转完毕后,向 index 发送加载完毕(发送的数据为一个函数)。
  3. 最后 index 执行这个函数。

示例:
index
Taro.navigateTo({ url:/pages/school/detailed/index, success: () => { event.once(EventName.BuildingDetailed, (triggerDataFunc)=>{ triggerDataFunc(“发送的数据”) }) } })

detailed:
useEffect(() => { event.trigger(EventName.BuildingDetailed, (arg)=>{ console.log("data",arg) }) }, []);

@ZakaryCode
Copy link
Contributor

ZakaryCode commented Feb 19, 2024

可以使用wx.onAppRoute做一个监听

可以像这里说的那样直接使用 onAppRoute 方法,由于这是一个非公开 API,一直没有添加到 Taro 的类型提示和文档当中。

#7470

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants