Angular - I need avoid the destroy of the component

Viewed 9772

I have a component with an <iframe> element inside.

This component is called for a route.

When the component is called, the <iframe> element reloads the content, but I need load this content only once. For this, I will need to cancel the destroy of this component.

I tried moving this <iframe> to <body> element when the onDestroy is called and reuse this when this component is called again, but when you move an <iframe> to another parent, his content is reloaded.

Does someone have an idea of how do this?

2 Answers

I believe what you are looking for would be the RouteReuseStrategy.

Instead of destroying + re-creating components on consequent visits, you can re-use your routes.

See this answer for the implementation.

I solved my problem!

I created a component with position: fixed, one level up of router-outlet component.

When the user go in of the route, the ngAfterViewInit will be call the fixed component to show the iframe.

When the user go out of the route, the OnDestroy will be call the method to hide the iframe.

Related