2016-08-22 29 views
3

我想將一些配置信息傳遞給我的aurelia功能,但我不知道如何。我沒有找到關於aurelia文檔如何做的文檔。Aurelia將配置傳遞給插件或功能

我的特點

Main.js

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

Index.js

export function configure(config) { 
    config.globalResources('./scrollbar'); 
} 

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 
    } 
} 

回答

6

feature方法(和plugin法)會接受你的具體功能配置參數:

main.js

let scrollbarConfig = { foo: 'bar' }; 

aurelia.use 
    .standardConfiguration() 
    .feature('aurelia-scrollbar', scrollbarConfig); 

在您的功能的配置方法,在註冊的配置對象集裝箱。

奧裏利亞-滾動條/ index.js

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

凡是取決於配置可以使用容器來檢索它:

奧裏利亞-滾動條/ scrollbar.js

@inject(Element, 'scrollbar-config') 
export class Scrollbar { 
    constructor(element, scrollbarConfig) { 
    ... 
    } 
    ... 
} 

GitHub問題將此信息添加到Aurelia文檔中:https://github.com/aurelia/framework/issues/570