I'm just getting started with web-components and trying to abstract repeating HTML code to their own files, so not like I'm doing with <nav-bar> and <bootstrap-wrapper>, but more of a component based approach.
Now, I want to structure my project in such a way that my template is sent to a slot in index.html to render.
How I can render welcome.html inside of my index.html, also, how do I then navigate from welcome.html to another template
index.html
<!DOCTYPE html>
<html lang="en">
<body>
<bootstrap-wrapper>
<nav-bar></nav-bar>
<span slot="content"></span>
</bootstrap-wrapper>
</body>
<script src="actio.js"></script>
</html>
actio.js
customElements.define(
'nav-bar',
class NavBar extends HTMLElement {
connectedCallback() {
this.innerHTML = `
<nav class="nav">
<a class="nav-link" href="welcome.html">Home</a>
<a class="nav-link" href="enter-names.html">Enter Names</a>
<a class="nav-link" href="calculator.html">Calculator</a>
<a class="nav-link" href="history.html">History</a>
</nav>
`;
}
}
);
const template = document.createElement('template');
template.innerHTML = `
<div class="container">
<div class="row">
<div class="col-sm-12 col-md-8 col-lg-6">
<div class="jumbotron bg-dark text-white">
<p><slot name="content" /></p>
</div>
</div>
</div>
</div>
`;
customElements.define(
'bootstrap-wrapper',
class BootstrapWrapper extends HTMLElement {
constructor() {
super();
this.attachShadow({
mode: 'open'
}).appendChild(
template.content.cloneNode(true)
);
}
}
);
welcome.html
<template class="welcome">
<h1>Household Budget Calculator</h1>
<h3>No more arguments about how to divide your household expenses!</h3>
<h4>How it works:</h4>
<ol>
<li>Enter names</li>
<li>Fill household expenses</li>
<li>Each of you fills in their income</li>
<li>Hit Calculate</li>
<li>Enjoy a blissful partnership!</li>
</ol>
<button onclick="location.href='enter-names.html'"
type="button"
class="btn btn-primary">Enter Names</button>
</template>
Use
fetch(), which is an asynchronous function.In your
BoostrapWrapperclass, add a method:No need to include the code in a
<template>element unless you use Javascript code inside. In this latter case you'll need to create a temporary<template>element.You can then call the method with any HTML file.