typescript 从选项卡转到根页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42600734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Go to root page from a tab
提问by Rik
I would like to know how to go back to the rootPage defined in the appComponent
when using tabs. The setRoot method is not working as I expected. When it is used in a Tab page the navigation stack is not cleared. On the 'home page', the back button is visible instead of the navigation toggle and the title of the tab is shown.
我想知道如何返回appComponent
使用选项卡时定义的 rootPage 。setRoot 方法没有按我预期的那样工作。当它在标签页中使用时,导航堆栈不会被清除。在“主页”上,后退按钮是可见的,而不是导航开关,并且显示了选项卡的标题。
By default, pages are cached and left in the DOM if they are navigated away from but still in the navigation stack (the exiting page on a push() for example). They are destroyed when removed from the navigation stack (on pop() or setRoot()).
默认情况下,如果页面被导航离开但仍在导航堆栈中(例如 push() 上的退出页面),则页面会被缓存并留在 DOM 中。当从导航堆栈中移除时(在 pop() 或 setRoot() 上),它们将被销毁。
The statement above gives me the expectation that when I use setRoot pages are cleared from the cache. This seems to be true when it is used in a normal page but not in a tab.
上面的语句让我期望当我使用 setRoot 页面时会从缓存中清除。当它在普通页面中使用而不是在选项卡中使用时,这似乎是正确的。
In the class of the tab page, there is a function that sets the root page to home when the button is clicked.
在标签页的类中,有一个函数,当按钮被点击时,将根页面设置为主页。
goToHome() {
this.navCtrl.setRoot(HomePage);
}
How can I make sure that when we are returned to the homePage there is no back button and the title of the home is used that is available in the HTML template of the component.
我如何确保当我们返回主页时没有后退按钮,并且使用组件的 HTML 模板中可用的主页标题。
回答by sebaferreras
Just like you can see in the docs
就像你在文档中看到的一样
Notice that each
<ion-tab>
binds to a[root]
property, just like in the Navigation section above. That is because each<ion-tab>
is really just a navigation controller. This means that each tab has its own history stack, andNavController
instances injected into children@Components
of each tab will be unique to each tab
请注意,每个都
<ion-tab>
绑定到一个[root]
属性,就像上面的导航部分一样。那是因为每个<ion-tab>
实际上只是一个导航控制器。这意味着每个选项卡都有自己的历史堆栈,NavController
注入@Components
每个选项卡子项的实例对于每个选项卡都是唯一的。
So when setting a page as root, you're using the navigation stack from that tab, and not the one of the entire app. That's why you need to get the main navigation stack by doing:
因此,当将页面设置为 root 时,您使用的是该选项卡中的导航堆栈,而不是整个应用程序中的一个。这就是为什么您需要通过执行以下操作来获取主导航堆栈的原因:
constructor(private app: App,...) {...}
And then
接着
yourMethod(): void {
this.app.getRootNav().setRoot(YourPage);
}
回答by MIB
just try this it work great for me
试试这个它对我很有用
this.childNavCtrl.setRoot(HomePage);