🔍 Understanding loadComponent
Function
• We will now break down the JavaScript
function step by step...
📦 loadComponent Breakdown
export function loadComponent(name) Defines a function named 'loadComponent'.
{ It takes 'name' (like 'login') as input.
fetch(`components/${name}/$ 1. Fetches the HTML file of the component.
{name}.html`)
.then(res => [Link]())
2. Converts the fetched content to text.
.then(html => { 3. Injects HTML into the <div id='app'>.
4. Dynamically adds the component's JS file.
[Link]('app').inner This lets you load HTML+JS on the fly!
HTML = html;
const script =
[Link]('script');
[Link] = `components/$
{name}/${name}.js`;
[Link](script);
});
}