简体   繁体   English

(Ionic4)如何动态转到上一页?

[英](Ionic4) How to dynamically go to previous page?

I am currently updating my company's app from Ionic v3 to Ionic v4. 我目前正在将公司的应用程序从Ionic v3更新到Ionic v4。 I am stuck on trying to figure out how to dynamically go back to the previous page the user was on. 我一直试图找出如何动态返回到用户所在的上一页的方法。 I keep finding answers of this.router.navigate(['pagename']) or this.navCtrl.navigateBack('pagename') , however, there are multiple entry points to a map in our app which is where the problem lies. 我一直在寻找this.router.navigate(['pagename'])this.navCtrl.navigateBack('pagename')答案,但是,问题出在我们应用程序中的地图上有多个入口点。 We used to use this.navCtrl.pop which would bring the user back to whatever page they came from... is there an equivalent to this since I don't know which page the user will be coming from? 我们曾经使用过this.navCtrl.pop ,它将使用户回到他们来自的任何页面...是否存在与此等效的功能,因为我不知道用户来自哪个页面?

You must use these solutions. 您必须使用这些解决方案。 These will work on your ionic 4 project. 这些将适用于您的ionic 4项目。

First import { Location } from '@angular/common'; 首先import { Location } from '@angular/common';

private location: Location,

gotoBack() {
this.location.back();
}

Second is 第二个是

import { NavController } from '@ionic/angular';

constructor(
private navCtrl: NavController
) { }

gotoBack() {
this.navCtrl.back();

} }

From the docs , seems like you could still use the pop() method in Ionic 4: docs看来,您仍然可以在Ionic 4中使用pop()方法:

pop 流行音乐

Description : Pop a component off of the navigation stack. 描述 :将组件从导航堆栈中弹出。 Navigates back from the current component. 从当前组件导航回去。

Signature : pop(opts?: NavOptions | null | undefined, done?: TransitionDoneFn | undefined) => Promise<boolean> 签名pop(opts?: NavOptions | null | undefined, done?: TransitionDoneFn | undefined) => Promise<boolean>

So it'd be something like this.navCtrl.pop(); 因此,它就像这样this.navCtrl.pop();

Note : Please notice that this may or may not work based on how you're handling the navigation (from the outlet, from a modal component, ...) so if this doesn't work please add some more information about how you're navigating to that map page in your app so that we can see what other options are available based on that. 注意 :请注意,根据您处理导航的方式(从插座,模态组件等),这可能会或可能不会起作用,因此,如果这不起作用,请添加有关您的导航方式的更多信息重新导航到您应用中的地图页面,以便我们可以基于此查看其他可用选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM