I have a component (a parallax header) that I am importing into certain pages in an Ionic4 app.
In order for the parallax to work, I need to reference an element inside of it.
Which works fine until I have more than one page that has used the custom component, at which point it access the First instance of the component the app produced.
So, page2 references page1’s component.
My work around is to put the component inside a div with a unique element ID specific to that page.
I can then reference it like so:
this.header = document.getElementById('header_contact').getElementsByClassName('header-image');
Here is the markup:
<custom-header id="header_contact" [header]="header_data"><custom-header>
The page is a Contact page and header-image is the element inside the component I need to access.
This works but seems unnecessary and I am wondering if any other options come to mind?
have the custom component imported via a components.module.
( I have had no luck making this work as a directive in Ionic 4 )
My current options are
- Use as above with the element ID.
- Use a service to reference the current working instance of the custom component.
- Iterate through the getElementsByClassName list and look for its ‘presence’ in the view …?
- Destroy each instance of the custom component on page leave.