简体   繁体   English

Ionic Angular 路由器组件缓存问题

[英]Ionic Angular router component caching issue

Currently, I am developing an Ionic 5 application with Angular 9 (Single page app) and I am having some issues regarding the routing/component caching (At least that is what I think) and would need a bit of help.目前,我正在使用 Angular 9(单页应用程序)开发 Ionic 5 应用程序,并且我在路由/组件缓存方面遇到了一些问题(至少我是这么认为的)并且需要一些帮助。 I will try to explain my situation the best that I can so that you can grasp the concept and the issue I am battling with.我将尽我所能解释我的情况,以便您了解我正在与之斗争的概念和问题。

I have one page on which I have 2 <ion-tab-bar> controls / tabs (Active and Inactive).我有一页上有 2 个<ion-tab-bar>控件/选项卡(活动和非活动)。 On the Active tab, I am listing the products from my database which have the 'active' flag set on them in the DB, and on Inactive I am listing the opposite ones.在“活动”选项卡上,我列出了我的数据库中在数据库中设置了“活动”标志的产品,而在“非活动”上,我列出了相反的产品。 On these tabs, the products are shown in a list-oriented way, styled as cards/card components.在这些选项卡上,产品以面向列表的方式显示,样式为卡片/卡片组件。

After I click on a card I navigate the user to a new page (/active/:id) on which I show the product details.单击卡片后,我将用户导航到显示产品详细信息的新页面 (/active/:id)。 Among other features, there is a button that marks the product as Inactive.除其他功能外,还有一个将产品标记为非活动状态的按钮。 By clicking this button I am making an api call which marks the product as Inactive in the db.通过单击此按钮,我正在进行 api 调用,该调用将产品在数据库中标记为非活动。 Also, this page contains a <ion-back-button> button component which brings me back to the previous page that I was on.此外,此页面包含一个<ion-back-button>按钮组件,它可以让我回到上一页。

The problem问题

My issue is, that when I mark the product as Inactive, it changes its state in the db, but when I click on the back button and return to the Active tab, the product which I marked as Inactive is still under the Active tab, and I noticed that none of the lifecycle methods are being called , nor is the api call being executed in order to refresh the list.我的问题是,当我将产品标记为非活动时,它会在数据库中更改其 state,但是当我单击后退按钮并返回活动选项卡时,我标记为非活动的产品仍在活动选项卡下,我注意到没有调用任何生命周期方法,也没有执行 api 调用以刷新列表。

After this, when I go to the Inactive tab, now my product is shown here, and when I switch to the Active tab again, it's no longer under Active tab, what I would expect in the first place.在此之后,当我将 go 转到非活动选项卡时,现在我的产品显示在此处,当我再次切换到活动选项卡时,它不再在活动选项卡下,这是我最初所期望的。

This behavior does not only happen when I navigate through the back button, but it also happens when I navigate through the header navigation (Because of this, I think this is not related to rendering the page from location history).这种行为不仅在我通过后退按钮导航时发生,而且在我通过 header 导航时也会发生(因此,我认为这与从位置历史记录呈现页面无关)。

I'm posting an image below, which visually represents the problem.我在下面发布了一张图片,它在视觉上代表了这个问题。

Any suggestions/guidelines would be appreciated.任何建议/指南将不胜感激。 Thanks in advance.提前致谢。

在此处输入图像描述

Background:背景:

Ionic uses a route strategy that preserves the UI state while navigating. Ionic 使用路由策略,在导航时保留 UI state。 For that reason, Ionic provides the life cycle methods to properly watch for the view changes.出于这个原因,Ionic 提供了生命周期方法来正确地观察视图的变化。 Unfortunately, these life cycle methods only work when navigating between routes of the same <ion-router-outlet> .不幸的是,这些生命周期方法仅在同一<ion-router-outlet>路由之间导航时才有效。

In the Ionic app, <ion-tabs> internally uses an <ion-router-outlet?在 Ionic 应用程序中, <ion-tabs>内部使用<ion-router-outlet? . . So the life cycle methods declared inside the individual tab component are only called when navigating between the tabs.因此,在单个选项卡组件中声明的生命周期方法仅在选项卡之间导航时才会被调用。

In your app, when you are navigating to /active/:id , you are essentially moving out of the component which contains <ion-tabs> and going to the component mentioned in the /active/:id route.在您的应用程序中,当您导航到/active/:id时,您实际上是从包含<ion-tabs>的组件移出并转到/active/:id路由中提到的组件。 This change of view happens in the <ion-router-outlet> you have specifically used in your project.这种视图变化发生在您在项目中专门使用的<ion-router-outlet>中。

So Coming to the solutions, there are two approaches you can try.因此,谈到解决方案,您可以尝试两种方法。

1. Use Ionic Lifecycle methods in the component where <ion-tabs> is used 1.在使用<ion-tabs>的组件中使用Ionic Lifecycle方法

In the component where you have used <ion-tabs> , add ionViewWillEnter method.在您使用<ion-tabs>的组件中,添加ionViewWillEnter方法。 Since you are using ngRx, you can call an action to refresh the products list in the application state.由于您使用的是 ngRx,因此您可以调用一个action来刷新应用程序 state 中的产品列表。 This way, when you are on a specific tab - Active / Inactive, you will have the refreshed products data readily available on the UI.这样,当您在特定选项卡上 - 活动/非活动时,您将可以在 UI 上轻松获得刷新的产品数据。

2. Subscribe on the router URL 2.在路由器URL上订阅

In this approach, you will have to subscribe to the router URL in your Active and Inactive tabs.在这种方法中,您必须在活动和非活动选项卡中订阅路由器 URL。 Whenever the UI navigates to that specific route, we can watch on the changes and refresh the products list.每当 UI 导航到该特定路线时,我们都可以观察更改并刷新产品列表。

Since I was not using ngRx in my project, I had to use the second approach to refresh the data on my app.由于我没有在我的项目中使用 ngRx,我不得不使用第二种方法来刷新我的应用程序上的数据。 The complete code for the second approach is mentioned here . 此处提到了第二种方法的完整代码。

Let us know which approach worked for you.让我们知道哪种方法适合您。

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

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