To access elements rendered by component we need to use template property.
const h1Element = this.template.querySelector('h1');
Note - Dont use Id selector with query selector in LWC
To access elements rendered by component we need to use template property.
Note - Dont use Id selector with query selector in LWC
What is DOM ?
DOM - Document Object Model is a programming API for HTML or XML component, It defines logical structure of documents and the way document is accessed and manipulated. Basically the DOM is tree structure representation of web page.
What is Shadow DOM ?
Shadow DOM brings encapsulation concept to DOM. It enables to link hidden separate DOM to an element.
Benefits: DOM query, CCS rules and event propagation does not cross shadow boundary, hence creating encapsulation.
Basically when we try fetch all the DOM using query selectors, we will not get DOM elements for child component in parent component.
When we apply style sheet to DOM of parent component, there will be no impact to child component DOM.
Event propagation will not work from child to parent or vice versa by default.
How to attach shadow DOM to element ?
<script>
const element = docuement.queryselector('#elementId');
const shadowRoot = element.attachShadow({mode:'open'});
shadowRoot.innerHTML = '<p>I am a shadow DOM</p>'
</script>
Adding one LWC component within body of another LWC component is known as component composition. It allows building complex component from simper building block component.
How to refer child component from parent component ?
1. childComponent <c-child-component></c-child-component>
2. childComponentDemo <c-child-component-demo></c-child-component-demo>
3. childComponentLWC <c-child-component-l-w-c></c-child-component-l-w-c>
This naming convection known as kebab case. Replace capital letter with small and prefixed with hypen.
Advantages - Defining smaller component will make testing easy and reusability.
There are 2 types of template looping in LWC