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

如何知道来源页面 #1494

Closed
hyacinthus opened this issue Dec 13, 2018 · 8 comments
Closed

如何知道来源页面 #1494

hyacinthus opened this issue Dec 13, 2018 · 8 comments
Assignees
Labels
enhancement New feature or request

Comments

@hyacinthus
Copy link

如果重复提问了不好意思,github 的 issue 搜中文几乎没法搜,翻了半天没翻到。

我想知道当前页面是从哪个页面点过来的。用来智能的防止超过10层跳转。

在 wepy 里边可以通过默认的 payload data 里的 from 字段得到。

Taro 里有没有办法呢。

@taro-bot
Copy link

taro-bot bot commented Dec 13, 2018

欢迎提交 Issue~

如果你提交的是 bug 报告,请务必遵循 Issue 模板的规范,尽量用简洁的语言描述你的问题,最好能提供一个稳定简单的复现。🙏🙏🙏

如果你的信息提供过于模糊或不足,或者已经其他 issue 已经存在相关内容,你的 issue 有可能会被关闭。

Good luck and happy coding~

@nyrf
Copy link

nyrf commented Dec 13, 2018

使用 Taro.getCurrentPages() 可查看:https://developers.weixin.qq.com/miniprogram/dev/framework/app-service/route.html

getCurrentPages() 函数用于获取当前页面栈的实例,以数组形式按栈的顺序给出,
第一个元素为首页,最后一个元素为当前页面。

@hyacinthus
Copy link
Author

@nyrf 谢谢 确实是可以取到的。。

    let pages = Taro.getCurrentPages()
    console.log(pages)
    if (pages.length > 1 && pages[pages.length - 2].route === 'pages/index') {
      console.log('from index,and back.')
      Taro.navigateBack()
      return
    }

但是这样也未免太麻烦了……
而且整个页面的数据太多了,我只看到了route,没有找到类的名字之类的更好写的标志。

不知道有没有像 wepy 那样的快捷方式。

@nyrf
Copy link

nyrf commented Dec 14, 2018

这个好像现在没有,你可自己写成一个方法,然后直接在需要的页面调用,也是可以的,wepy只是帮你把这个方法写好了。

@hyacinthus
Copy link
Author

嗯 我现在是可以用的
如果常用的话可以考虑放 $router 里就相当方便了,比如

if (this.$router.source === 'Index') {...}

顺便说下我的使用场景是这样,看看有没有更好的做法:

我的详情页面有可能是扫码,分享点进来的。我在详情页面的最底端会放回到首页的按钮,如果判断是从列表过来的,我就会调用返回,如果判断是从其他地方过来的,我就会调用 navigate ,这样页面不会爆栈。。

@luckyadam
Copy link
Member

可以改进一下~

@luckyadam luckyadam added the enhancement New feature or request label Dec 17, 2018
@luckyadam luckyadam self-assigned this Dec 17, 2018
@shenghanqin
Copy link
Collaborator

我还见过把页面监控的方法监控起来,超过10级就用自定义的方式来实现跳转。效果还行。

@taro-bot
Copy link

taro-bot bot commented Nov 11, 2019

Hello~

您的问题楼上已经有了确切的回答,如果没有更多的问题这个 issue 将在 15 天后被自动关闭。

如果您在这 15 天中更新更多信息自动关闭的流程会自动取消,如有其他问题也可以发起新的 Issue。

Good luck and happy coding~

@taro-bot taro-bot bot removed the to be closed label Dec 3, 2019
@taro-bot taro-bot bot closed this as completed Dec 3, 2019
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

4 participants