【问题标题】:How to reference specific elements in Aurelia custom element's <slot>?如何在 Aurelia 自定义元素的 <slot> 中引用特定元素?
【发布时间】:2017-08-10 16:57:42
【问题描述】:

我想在 Aurelia 中创建一个自定义字段集,我需要在“slot”标签中设置标签元素的样式(宽度)(参见下面的示例用法)。如何访问这些元素?到目前为止我所拥有的是

<template>
  <require from="./ib-fieldset.css"></require>
  <fieldset style.bind="style">
    <legend>${title}</legend>
    <slot></slot>
  </fieldset>
</template>

import {bindable} from 'aurelia-framework';
import * as $ from 'jquery';

export class IbFieldset {
  @bindable title: string;
  @bindable top: number;
  @bindable left: number;
  @bindable labelWidth: number;

  style: string;

  attached() {
    this.title = ` ${this.title} `;
    this.style = `position: absolute; top: ${this.top}px; left: ${this.left}px;`;
  }
}

我是这样用的:

  <ib-fieldset title="Address" top="100" left="200" labelWidth="100">
    <label for="firstName">First name:</label>
    <input id="firstName" type="text">

    <label for="lastName">Last name:</label>
    <input id="lastName" type="text">
  </ib-fieldset><br>

我尝试使用 jquery,但我不知道如何仅选择字段集组件中的元素(而不是可以包含其他字段集的整个页面)。

【问题讨论】:

    标签: aurelia custom-element selectors-api


    【解决方案1】:

    像 Aurelia 中的大多数情况一样,不需要 jQuery。

    GistRun:https://gist.run/?id=aa1e8239736e0de11f73116966af9ac9

    fieldset.html

    <template>
      <fieldset style="position: absolute; top: ${top}px; left: ${left}px;">
        <legend>${title}</legend>
        <slot ref="slotElement"></slot>
      </fieldset>
    </template>
    

    fieldset.js

    import {bindable} from 'aurelia-framework';
    
    export class IbFieldset {
      @bindable title: string;
      @bindable top: number;
      @bindable left: number;
      @bindable labelWidth: number;
    
      attached() {
        this.title = ` ${this.title} `;
    
        let labels = this.slotElement.querySelectorAll('label');
    
        for (let label of Array.from(labels)) {
            label.setAttribute('width', `${labelWidth}px`);
        }
      }
    }
    

    consumer.html

    <require from="./fieldset"></require>
    
    <ib-fieldset title="Address" top="100" left="200" label-width="100">
      <label for="firstName">First name:</label>
      <input id="firstName" type="text"/>
    
      <label for="lastName">Last name:</label>
      <input id="lastName" type="text"/>
    </ib-fieldset><br>
    

    注入元素:

    import {inject} from ‘aurelia-framework’;
    
    @inject(Element)
    export class IbFieldset {
        constructor(element) {
            this.element = element;
        }
    
        attached() {
            let labels = this.element.querySelectorAll('label');
            ……
        }
    

    【讨论】:

    • 感谢 Jeff,但我如何设置标签的宽度(请参阅我的“labelWidth”属性)?
    • 由于插槽中的元素是在消费者中定义的,因此最简单的方法是简单地将它们设置在那里。请参阅我的更新答案。如果您正在创建一个通用元素并且真的希望它设置标签宽度,请告诉我,我会告诉您如何做到这一点。
    • 嗨,杰夫,是的,字段集应该处理这个问题。
    • 我更新了我的答案,向您展示了一种可以让自定义元素在内部设置标签宽度的方法。要点是您使用ref=slotElement 获得对插槽元素的引用,并在视图模型的attached 方法中使用它来设置标签宽度。顺便说一句,您的原始代码在 html 中引用了 labelWidth 而不是 label-width。在 html 元素中,camel cased javascript bindable 被引用为 kabob cased 属性。
    • 是的,我要找的是querySelectorAll('label');。而且我没有让ref&lt;slot&gt; 标签上工作,我不得不在它周围放置一个&lt;div&gt; 然后引用div。非常感谢,杰夫!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-27
    • 2018-03-13
    • 1970-01-01
    • 1970-01-01
    • 2016-10-06
    • 2015-08-07
    相关资源
    最近更新 更多