简体   繁体   English

如何在另一个中包含自定义聚合物元素

[英]How to include custom polymer element inside another

How to include custom polymer 3 element inside another? 如何在另一个内部包含自定义聚合物3元素? Let's say I have two components: 假设我有两个组成部分:

1) Parent-component 2) Child-component 1)父级组件2)子级组件

I would like to include and use child component inside parent component. 我想在父组件中包括并使用子组件。 The question is: where should I put my child element and how to configure import path? 问题是:我应该在哪里放置子元素以及如何配置导入路径?

In your parent element add the import declaration of the child element: 在您的父元素中添加子元素的导入声明:

import './child-element.js';

Then, add a reference of the imported element in the parent's template: 然后,在父级模板中添加对导入元素的引用:

<child-element></child-element>

Your child element could be like: 您的子元素可能类似于:

import { PolymerElement, html } from '@polymer/polymer/polymer-element.js';

class ChildElement extends PolymerElement {
  static get template() {
    return html`
      <style>
        :host {
          display: block;
        }
      </style>
      <div>This is you child element</div>
    `;
  }
}

window.customElements.define('child-element', ChildElement);

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

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