I want to set the value of an ion-input field when the page is loaded.
I'm trying to get the element from the shadowDom
using the shadowRoot querySelector
.
It is a stencil project with ionic. The component uses the shadowDOM
.
When I try to get the value my IDE says:
The nameEl:
const nameEl = this.el.shadowRoot.querySelector('#name');
The value will be set correctly, but I was wondering how I could fix this error message.
EDIT: Answer as suggested by Ash is correct. And like Ash mentioned there are two ways of implementing this solution. But I was wondering if there is any difference between the solutions?
const nameEl = this.el.shadowRoot.querySelector('#name') as HTMLInputElement;
or
const nameEl: HTMLInputElement = this.el.shadowRoot.querySelector('#name');
EDIT:
The solution of course also work as one-liners as in Ash his example.
(this.el.shadowRoot.querySelector('#name') as HTMLInputElement).value
And in my case the input is a ionic input:
(this.el.shadowRoot.querySelector('#name') as HTMLIonInputElement).value