Aurelia-dialog在自定义元素上使用attach-focus

Aurelia-dialog using attach-focus on custom element

本文关键字:attach-focus 元素 自定义 Aurelia-dialog      更新时间:2023-09-26

我试图将attach-focus="true"传递给自定义元素的内部元素之一,以便在aurelia-dialog打开时正确的元素将接收焦点。

自定义元素:enum-list.html

<template>
  <label class="control-label">${label} DEBUG: ${attach-focus}</label>
  <select class="form-control" value.bind="value" attach-focus.bind="attach-focus">
    <option if.bind="data" repeat.for="code of data | keys" value="${code}">${data[code]}</option>
  </select>
</template>

定制元素:enum-list.js

import { bindable, bindingMode } from 'aurelia-framework';
export class EnumListCustomElement {
  @bindable label;
  @bindable data;
  @bindable attach-focus; // <-- Maybe the source of the error?
  @bindable({ defaultBindingMode: bindingMode.twoWay }) value;
}

对话模板:edit-locale.html:

<template>
  <ai-dialog>
    <ai-dialog-header class="modal-header modal-header-success">
      <h4 class="modal-title">Edit Locale</h4>
    </ai-dialog-header>
    <ai-dialog-body>
      <form>
        <enum-list attach-focus="true" label="Language" data.bind="core.enums.SystemLanguage" value.bind="sch_lang"></enum-list>
        <enum-list label="Currency" data.bind="core.enums.CurrencyCode" value.bind="sch_currency"></enum-list>
      </form>
    </ai-dialog-body>
    <ai-dialog-footer>
      <button type="button" click.trigger="dialogController.cancel()">Cancel</button>
      <button type="button" click.delegate="dialogController.ok()">Save</button>
    </ai-dialog-footer>
  </ai-dialog>
</template>

实例化(从我的VM js):

this.dialogService.open({ viewModel: EditLocale, model: this.record, lock: true }).then(response => {

模态对话框加载良好,如果我从edit-locale.js中的附加焦点和自定义元素中删除破折号。但与dash,我得到一个错误:Uncaught SyntaxError: Unexpected token import。我觉得仪表盘有点干扰,但我不知道怎么修理。

我的偏好是修复它,以便自定义控件的实例化具有标准参数attach-focus="true"(带破折号),以便它与INPUT和SELECT等正常元素一致。

你对错误的来源是正确的,你不能有一个包含破折号的property-name。因为它读作property - name

在aurelia中有一个约定(链接到文档,搜索破折号大小写)将属性和元素名称从破折号表示法映射到骆驼大小写表示法,所以如果在你的模型中你将你的可绑定属性命名为@bindable attachFocus -你将能够在你的视图中使用它作为attachment -focus.bind="true"。

还要确保在视图中<require>自定义元素/属性,或者在配置aurelia时使它们全局可用。