简体   繁体   English

嵌套Web组件并将其与常规HTML混合

[英]Nesting web components and mixing it with regular HTML

Is it possible to create independent web components with Stencil or Polymer and then nest them one within another? 是否可以使用Stencil或Polymer创建独立的Web组件,然后将它们彼此嵌套? Also, is it possible to mix all of that with regular HTML content? 另外,是否可以将所有内容与常规HTML内容混合在一起?

We would have 3 independent components here, with unique styles and functionalities. 我们这里将具有3个独立的组件,具有独特的样式和功能。

  • <comp-card>
  • <comp-avatar>
  • <comp-text-box>

They should be nested inside of regular HTML like this for example: 它们应嵌套在常规HTML中,例如:

<comp-card>
    <comp-avatar>
        <img src="...">
    </comp-avatar>
    <comp-text-box>
        <p>lorem ipsum</p>
    </comp-text-box>
</comp-card>

Is this possible to achieve this today with native web components? 今天是否可以通过本机Web组件实现这一目标?

I'm not sure about native web components but it definitely is possible within polymer. 我不确定本机Web组件,但在聚合物中肯定是有可能的。 For instance my main polymer app file contains: 例如,我的主要聚合物应用程序文件包含:

<module-landing>
  <firebase-content path="pages/home/tagline"></firebase-content>
</module-landing>

and within my custom module-landing there is: 在我的自定义模块着陆范围内:

  <template>

<style>
   ...
</style>


<module-inner size="1140px">
    <h1 id="title">
      <slot></slot>
    </h1>
    <img id="goDown" class="icon" src="" on-click="raiseCurtain">
</module-inner>

</template>


<script>
/**
 * `module-landing`
 * Full screen landing element with an arrow that scrolls itself out of the way, content is loaded through Firebase (requires firebase to be initialised in main app)
 *
 * @customElement
 * @polymer
 * @demo demo/index.html
 */
class ModuleLanding extends Polymer.Element {
  static get is() { return 'module-landing'; }
  static get properties() {
    return {
      height: {
        type: Number
      }
    };
  }
  ready(){
    super.ready();
    this.height = this.offsetHeight;
    this.$.goDown.src = this.resolveUrl("assets/white-down-arrow.png");
  }

  raiseCurtain(){
    window.scroll({ top: this.height, left: 0, behavior: 'smooth' });        
  }

}

window.customElements.define(ModuleLanding.is, ModuleLanding);

(note the slot tags indicating where content nested within the custom element in the main file should appear, and the module-inner custom element inserted within the landing). (请注意插槽标签,该标签指示嵌套在主文件中自定义元素中的内容应出现的位置,而模块内部的自定义元素将插入到着陆中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM