首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Aurelia自定义元素与对话框

Aurelia自定义元素与对话框
EN

Stack Overflow用户
提问于 2016-11-03 10:43:21
回答 1查看 427关注 0票数 0

我在创建自定义元素时遇到了困难,该元素将用于

代码语言:javascript
运行
复制
<shimmy-dialog type="video" href="/test">Hi</shimmy-dialog>

种子元素将用href替换这段代码,单击href时应该弹出一个特定类型的对话框。

在我试图打开对话框之前,一切似乎都进行得很顺利。这是我得到错误的时候

未处理的拒绝TypeError:无法将属性'bindingContext‘设置为null,有时我确实会发现Aurelia错误有点简单。

我怀疑这与没有视图的元素有关。

代码如下

代码语言:javascript
运行
复制
enum DialogType {
    video = 1,
    iframe
};

@inject(Bcp, DialogController)
export class ShimmyDialogModel {
  private type : DialogType;

  constructor(private bcp: Bcp, private controller : DialogController){
    console.log("here");
  }

  async activate(state){
    this.type = state['type'];
  }

  get isVideo() : boolean {
    return this.type == DialogType.video;
  }

  get isIframe() : boolean {
    return this.type == DialogType.iframe;
  }
}

@noView
@processContent(false)
@customElement('shimmy-dialog') 
@inject(Element, App, Bcp, DialogService)
export class ShimmyDialog {
  @bindable public type : string;
  @bindable public href;
  @bindable public name;
  private originalContent : string;

  constructor(private element: Element, private app: App, private bcp: Bcp,
              private dialogService: DialogService) {    
    this.originalContent = this.element.innerHTML;
  }

  bind() {
    this.element.innerHTML = '<a href="#">' + this.originalContent + '</a>';
  }

  attached() {
    let self = this;
    this.type = this.element.getAttribute("type");
    let dialogType = DialogType[this.type];
    this.element.children[0].addEventListener("click", function(){ 
      if(dialogType == DialogType.iframe) {
        self.dialogService.open({ viewModel: ShimmyDialogModel, model: {'type' : dialogType}}).then(response => {
        });
      }
      else if(dialogType == DialogType.video) {
        self.dialogService.open({ viewModel: ShimmyDialogModel, model: {'type' : dialogType}}).then(response => {
        });
      }
      return false;
    });
  }

  async typeChanged(newValue) {
    this.type = newValue;
  }

  async hrefChanged(newValue) {
    this.href = newValue;
  }
}

对话框的模板如下所示。

代码语言:javascript
运行
复制
<template>
  <require from="materialize-css/bin/materialize.css"></require>
  <ai-dialog>
    <ai-dialog-header>
    </ai-dialog-header>
    <ai-dialog-body>
      <div if.bind="isVideo">
          Video
      </div>
      <div if.bind="isIframe">
          IFrame
      </div>
    </ai-dialog-body>

    <ai-dialog-footer>
      <button click.trigger="controller.cancel()">Close</button>
    </ai-dialog-footer>
  </ai-dialog>
</template>

谢谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-04 09:11:21

我通过将类分离到它们自己的文件中来解决这个问题。Aurelia不喜欢在那里有两个导出类。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40399457

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档