I have the problem, that I need to show an iframe
within an Angular 6 app. This element should be present for all routes of a Module (lazy loaded). There are also other Modules in the app that don't need to show the iframe
.
So a global solution like this could work but is not what I'm looking for:
<!-- app.component.html -->
<app-iframe></app-iframe>
<router-outlet></router-outlet>
Let's say the routing in this Module looks like this:
const routes: Routes = [
{
path: '',
component: FirstComponent
},
{
path: 'route-2',
component: SecondComponent
},
{
path: 'route-3',
component: ThirdComponent
}
];
Now in my module I can't simply create a component like IframeComponent
and insert it into other components like:
<!-- first.component.html -->
<h1>Content of this component</h1>
<app-iframe></app-iframe>
This is because each time the another route of the module is loaded, the IframeComponent
is reloaded and so is the actual iframe
element.
I can't also cache the iframe
DOM element in a Service, as the re-insertion will still trigger a reload. See: How to prevent an iframe from reloading when moving it in the DOM.
My question is:
How can I specify a Component that is rendered one time as long as the Module lives, no matter which router from this Module is called.
The result could look like this:
<router-outlet></router-outlet>
<app-first></app-first>
<app-iframe></app-iframe>
And after going to module-url/route-3
:
<router-outlet></router-outlet>
<app-third></app-third> <!-- only this changed -->
<app-iframe></app-iframe>
And after going to another module:
<router-outlet></router-outlet>
<app-another-component></app-another-component>