0

我知道可以导出配置功能,也可以将组件注册为全局资源。但是,当您的组件提供可以被覆盖的“默认”配置时,您使用什么模式?这需要在配置阶段发生,并且需要应用于组件的所有实例。您是否使用带有静态成员(打字稿)或其他东西的类?或者,我可以创建配置实例、设置属性并将其以某种方式添加到 DI 容器中,以便所有依赖组件现在都获取该实例吗?

import {PagingConfig} from "./PagingConfig";

export class PaginationConfig extends PagingConfig {

    boundaryLinks = false;
    boundaryLinkNumbers = false;
    directionLinks = true;
    rotate = true;
    forceEllipses = false;
    maxSize: number = null;
}

这是我配置的 main.ts。如何从上面覆盖默认配置?

    import * as Promise from "bluebird";
    import {Aurelia} from "aurelia-framework";
    import {PaginationConfig} from "./components/shared/PaginationConfig";

    export function configure(aurelia: Aurelia):void {
      aurelia.use
            .standardConfiguration()
            .developmentLogging()
            .globalResources(["components/bar/InsightBarCustomElement",
                              "components/pagination/PaginationCustomAttribute"]);
4

1 回答 1

0

通过阅读这个文档解决了这个问题 https://github.com/aurelia/dependency-injection/blob/master/doc/article/en-US/dependency-injection-basics.md

基本上,注入配置的 aurelia 附加了容器实例,所以我所要做的就是实例化我的配置类型,进行更改并将其添加到容器中。像这样的东西

import * as Promise from "bluebird";
import { PaginationConfig } from "./components/shared/PaginationConfig";
import { Aurelia } from "aurelia-framework";
import { Container } from "aurelia-dependency-injection";


export function configure(aurelia: Aurelia): void {
  function configurePaginationCustomAttribute(): void {

    let paginationConfig = new PaginationConfig();

    paginationConfig.firstText = "«";
    paginationConfig.previousText = "‹";
    paginationConfig.nextText = "›";
    paginationConfig.lastText = "»";

    aurelia.container.registerInstance(PaginationConfig,paginationConfig);
  }

  configurePaginationCustomAttribute();

  aurelia.use
    .standardConfiguration()
    .developmentLogging()
    .globalResources(["components/bar/InsightBarCustomElement",
      "components/pagination/PaginationCustomAttribute"]);

  let area: string = aurelia.host.getAttribute("data-area");

  console.log("Setting  aurelia root", area);

  aurelia.start().then(a => a.setRoot(area + "/app/app"));

}
于 2017-05-05T18:41:07.453 回答