【发布时间】:2019-02-24 12:57:08
【问题描述】:
我目前正在尝试使用 StencilJS 创建一些 Web 组件。
现在我知道有 <slot /> 和命名槽以及所有这些东西。来自 React,我猜 slot 类似于 React 中的 children。你可以在 React 中使用孩子做很多事情。我经常做的事情:
- 检查是否提供儿童
- 迭代子项以对每个子项执行某些操作(例如,将其包装在带有类的 div 中等)
你会如何使用 slot/web components/stencilJS 做到这一点?
我可以使用
在 Stencil 中获取我的 Web 组件的主机元素@Element() hostElement: HTMLElement;
我像这样使用我的组件
<my-custom-component>
<button>1</button>
<button>2</button>
<button>3</button>
</my-custom-component>
我想渲染类似的东西
render() {
return slottedChildren ?
<span>No Elements</span> :
<ul class="my-custom-component">
slottedChildren.map(child => <li class="my-custom-element>{child}</li>)
</ul>;
}
亲切的问候
【问题讨论】:
标签: web-component stenciljs stencil-component