【问题标题】:ref on a slot element in Aurelia参考 Aurelia 中的插槽元素
【发布时间】:2016-09-07 10:27:57
【问题描述】:

在 aurelia 中,当在自定义元素中使用插槽时,是否可以以某种方式在插槽上使用 ref 元素(可能是 Aurelia 团队的新功能?,据说 github 问题发布在 SO 而不是 github 上)。

例子:

元素.html:

<template> 
    My super awesome element
    <slot ref="slotContent"></slot>
</template>

元素.ts

export class CollapsePanel
{
    private slotContent: Element;

    attached(): void
    {
        this.slotContent.classList.add("my-fancy-class");
    }
}

otherview.html

<element>
    <div>This is some element content!</div>
</element>

渲染 otherview.html

<element>
    My super awesome element
    <div class="my-fancy-class">This is some element content!</div>
</element>

目前有什么办法吗?

【问题讨论】:

    标签: aurelia


    【解决方案1】:

    您将强制组件的用户使用可以具有类属性的 html 元素。做这样的事情会更好吗:

    自定义元素的模板

    <template>
      <div ref="slotContent"><slot></slot></div>
    </template>
    

    代码:

    export class CustomElement
    {
        private slotContent: HTMLDivElement;
    
        attached(): void
        {
            this.slotContent.classList.add("my-fancy-class");
        }
    }
    

    使用自定义元素(使用 textnode 而不是 html 元素)

    <customElement>foo</customElement>
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-29
    • 1970-01-01
    • 1970-01-01
    • 2018-10-01
    • 1970-01-01
    • 2021-05-11
    相关资源
    最近更新 更多