我不确定本机 Web 组件,但在聚合物中肯定是可能的。例如,我的主要聚合物应用程序文件包含:
<module-landing>
<firebase-content path="pages/home/tagline"></firebase-content>
</module-landing>
在我的自定义模块登陆中有:
<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);
(注意插槽标签指示嵌套在主文件中的自定义元素中的内容应该出现的位置,以及插入到登陆中的模块内部自定义元素)。