Frontends
Web Components
Edit on GitHubThis handler module serves web components with the .webc
extension.
Install
npm install @primate/webc
Configure
Import and initialize the module in your configuration.
import webc from "@primate/webc";
export default {
modules: [
webc(),
],
};
Use
Create an web component in components
.
<script>
import Component from "@primate/webc/Component";
import PostLink from "./post-link.webc";
export default class PostIndex extends Component {
mounted(root) {
root.querySelector("h1").addEventListener("click",
_ => console.log("title clicked!"));
}
render() {
const { posts } = this.props;
return `<h1>All posts</h1>
${posts.map(post => new PostLink({post})).join("")}
`;
}
}
</script>
And another component for displaying post links.
<script>
import Component from "@primate/webc/Component";
export default class PostLink extends Component {
render() {
const { post } = this.props;
return `<h2><a href="/post/view/${post.id}">${post.title}</a></h2>`;
}
}
</script>
Create a route and serve the post-index
component.
import view from "primate/handler/view";
const posts = [{
id: 1,
title: "First post",
}];
export default {
get() {
return view("post-index.webc", { posts });
},
};
Your rendered web component will be accessible at http://localhost:6161/webc.
Configuration options
extension
Default ".webc"
The file extension associated with web components.