Aurelia - 撰写视图

Aurelia - compose view

我对这个框架完全陌生。浏览所有文档后,我已经使用 visual studio 和类型脚本成功配置了 Aurelia 框架。 我想知道如何在另一个视图中组合一个视图,并从其父视图初始化它的视图模型。

例如: 在导航框架中,我们有一个欢迎视图,显示名字和第二个名字以及提交按钮。 现在,我在其中创建了一个名为 MyApp 的路由,我想编写欢迎视图,并且想将名字和名字传递给它的视图模型。

请告诉我该怎么做? 这就是我的 Html MyApp 的样子:

<template>
    <import from='welcome'></import>
    <section class="au-animate">       
      <compose view-model="welcome"></compose>
    </section>
</template>

视图模型是这样的:

import {inject} from 'aurelia-framework';
import refWelcome = require("welcome");

@inject(refWelcome)
export class myApp {
    vm;
    title: string;
    constructor(refWelcome) {
        this.title = "My App Demo";
        this.vm = refWelcome;

        console.log(this.vm);
    }
}

这是欢迎视图的视图模型:

import {computedFrom} from 'aurelia-framework';

export class Welcome{
  heading = 'Welcome to the Aurelia Navigation App!';
  firstName = 'John';
  lastName = 'Doe';
  previousValue = this.fullName;

  constructor(fname: string, lname: string) {
      if (fname != null || lname != null) {
          this.firstName = fname;
          this.lastName = lname;
      }
  }
}

我认为您的代码中存在一些问题 -

  1. 您需要修正 myApp 标题的语法 属性。
  2. 按照惯例,您应该将 类 PascalCase 命名为。
  3. 您目前无法使用标准 HTML 导入,如果您需要自定义元素,则需要使用 require

要组合视图,您可以使用两种方法 -

  1. 编写自定义元素(不需要)

     <template>
         <section class="au-animate">       
           <compose view-model="./welcome"></compose>
         </section>
     </template>
    

不知道为什么没有反引号就不会显示

  1. 作为自定义元素

    <template>
        <require from='welcome'></require>
        <section class="au-animate">       
          <welcome></welcome>
        </section>
    </template>