typescript - Aurelia StageComponent - 如何将子组件渲染到 DOM 树中?

标签 typescript aurelia

如果我有一个 app.html 模板如下:

<template>
  <require from="./MyComponent"></require>
  <h1>${message}</h1>
  <my-component>test</my-component>
</template>

使用 MyComponent.ts :

export class MyComponent {
    myName : string;
}

和 MyComponent.html:

<template>
    MyComponent
</template>

下面的单元测试总是会失败:

import {App} from '../../src/app';
import {StageComponent} from 'aurelia-testing';
import {bootstrap} from 'aurelia-bootstrapper';

describe('the app', () => {
  var app ;
  beforeEach(() => {
    app = StageComponent
    .withResources('app')
    .inView('  <require from="./MyComponent"></require>' +
      '<h1>${message}</h1>' +
      '<my-component>test</my-component>')
    .boundTo(new App());
  } );

  it('says hello', (done) => {
    app.create(bootstrap).then( () => {
      var myComponent = document.querySelector('my-component');
      expect(myComponent.innerHTML).toContain('MyComponent');
      done();
    });

  });
});

请注意,StageComponent 正确地将模板中的 ${message} 替换为 app.html,但不会创建新的 MyComponent 实例。 在浏览器中运行时,生成的 DOM 是:

  <h1>Hello World!</h1>
  <my-component class="au-target" au-target-id="2">
    MyComponent
</my-component>

但是当在测试中通过 StageComponent 运行相同的代码时,生成的 DOM 是:

<h1>Hello World!</h1>
<my-component>test</my-component>

我错过了什么?

最佳答案

不确定,但我认为您需要这样做(或者添加 customElement 是可选的吗?)

import {customElement, bindable} from 'aurelia-framework';

@customElement('my-component')
export class MyComponent {
    @bindable myName : string;
}

然后

it("test case", done => 
     StageComponent 
        .withResources("./full/path/to/MyComponent")
        .inView("<my-component></my-component>"))
        .create(bootstrap)
        .then(() => {...})
        .then(done);

我不太清楚为什么要在 app.html 中添加相同的 html,这是为了证明它有效,但不适用于组件吗?

或者您要测试的是什么?

  • 您要测试应用 View 是否可以使用其组件正确呈现吗?
  • 或者自定义 MyComponent 是否正确呈现?

关于typescript - Aurelia StageComponent - 如何将子组件渲染到 DOM 树中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39306960/

相关文章:

angular - base 64 以 Angular (2+) 编码和解码字符串

typescript - 纯 JS 中的 ng2 @ViewChild(ren)/@ContentChild(ren) ?

javascript - JavaScript 中的路径映射?

typescript - 在 Jest 中,如何仅在特定测试中模拟外部模块?

typescript - 如何将 mixin 与 Typescript 和 React JSX (TSX) 结合使用

javascript - 如何在 Aurelia 中使用 bootstrap-select 或任何其他 js 控件?

javascript - Aurelia - 应用程序初始化 - 临时禁用属性更改通知

javascript - 在 Aurelia 中基于 InnerHtml 更改背景颜色

typescript - 属性更改时更新 Aurelia 绑定(bind)

aurelia - 确定是否在提示中作为对话框打开