Vanilla JS中的Web组件:相当于NGFOR和V-FOR?
我有一个在香草JS中定义的自定义组件:
class Article extends HTMLElement {
constructor() {
super();
}
connectedCallback() {
this.render();
}
render() {
this.innerHTML = /*html*/ `
<div>this.getAttribute("title")</div>
`;
}
}
customElements.define("custom-article", Article);
然后,我想从数组(特别是在HTML中)实例化这些组件:
这在Angular和vue.js(可能在其他JS框架中)都是微不足道的,但是我找不到有关Vanilla JS中相同功能的文档。
I have a custom component defined in vanilla JS:
class Article extends HTMLElement {
constructor() {
super();
}
connectedCallback() {
this.render();
}
render() {
this.innerHTML = /*html*/ `
<div>this.getAttribute("title")</div>
`;
}
}
customElements.define("custom-article", Article);
I then would like to instantiate these components from an array, specifically (in the HTML):
This is trivial to do in both Angular and Vue.js (and probably in every other JS framework), however I can't find documentation on the same functionality being implemented in Vanilla JS.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可悲的是,JS模板文字尚未具有这样的功能 - 在使用Vanilla JS WebComponents工作时,您会发现自己越来越少地使用HTML字符串工作。
这是香草JS中基于字符串的方法的示例,呈现出水果列表:
然而,对于要创建的简单html,放弃字符串的使用完全可以正常工作:
这也具有整洁的优势,您可以更轻松地将事件听众附加到动态创建的元素上。
Sadly, JS template literals don't come with such functionality yet - you're going to find yourself working less and less with HTML strings when working on vanilla JS webcomponents.
Here's an example of a string-based approach in vanilla JS, rendering a list of fruits:
Yet for simple HTML that is to be created, dropping the use of strings altogether works just fine:
This also has the neat advantage that you can much more easily e.g. attach event listeners to the dynamically created elements.
对Connexo的略有增强他的答案
Small enhancement on Connexo his answer