Aurelia将配置传递给插件或功能

all*_*ded 3 javascript aurelia

我想将一些配置信息传递给我的aurelia功能,但我不知道如何.我在aurelia docs中找不到关于这是怎么做的文档.

我的特色

Main.js

.feature('aurelia-scrollbar', config => {
    // I want to pass an object along this
    config.foo = { bar: 'yay' }
 })
Run Code Online (Sandbox Code Playgroud)

Index.js

export function configure(config) {
  config.globalResources('./scrollbar');
}
Run Code Online (Sandbox Code Playgroud)

Scrollbar.js

import Scrollbar from 'smooth-scrollbar';
import 'smooth-scrollbar/dist/smooth-scrollbar.css!';

export class ScrollbarCustomAttribute {
  static inject = [Element];

  constructor(element) {
    this.element = element;
  }

  attached() {
    Scrollbar.init(this.element); // I want to use the passed configuration option here
  }
}
Run Code Online (Sandbox Code Playgroud)

Jer*_*yow 7

feature方法(和plugin方法)会接受你的具体功能配置参数:

main.js

let scrollbarConfig = { foo: 'bar' };

aurelia.use
  .standardConfiguration()
  .feature('aurelia-scrollbar', scrollbarConfig);
Run Code Online (Sandbox Code Playgroud)

在功能的configure方法中,在容器中注册config对象.

Aurelia路上,滚动条/ index.js

export function configure(frameworkConfiguration, scrollbarConfig) {
  frameworkConfiguration.globalResources('./scrollbar');
  frameworkConfiguration.container.registerInstance('scrollbar-config', scrollbarConfig);
}
Run Code Online (Sandbox Code Playgroud)

依赖于配置的任何东西都可以使用容器来检索它:

Aurelia路上,滚动条/ scrollbar.js

@inject(Element, 'scrollbar-config')
export class Scrollbar {
  constructor(element, scrollbarConfig) {
    ...
  }
  ...
}
Run Code Online (Sandbox Code Playgroud)

GitHub问题将此信息添加到Aurelia文档:https://github.com/aurelia/framework/issues/570