使用什么模式在aurelia中提供自定义元素或属性的全局配置?

时间:2017-05-05 15:52:50

标签: aurelia aurelia-di

我知道可以导出配置功能并将组件注册为全局资源。但是当你的组件提供"默认"可以覆盖的配置?这需要在配置阶段发生,并且需要应用于组件的所有实例。你是否使用静态成员(打字稿)或其他类?或者,我可以创建配置的实例,设置属性并以某种方式将其添加到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"]);

1 个答案:

答案 0 :(得分: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"));

}