自定义元素

时间:2016-03-10 02:21:09

标签: aurelia

这是我的自定义元素:

import {bindable, inject, noView, ViewCompiler, ViewSlot, Container, ViewResources} from 'aurelia-framework';
import _findIndex from 'lodash/findIndex';

let view = '<require from="./menu-bar-dd-item"></require>' +
            '<require from="./menu-bar-link-item"></require>' +
            '<ul class="nav navbar-nav">';

@noView
@inject(ViewCompiler, ViewSlot, Container, ViewResources)
export class MenuBar {
    @bindable router;

    constructor(vc, vs, container, resources){

        this.menuItems = [];

        this.createView();

        let viewFactory =  vc.compile(view, resources),
            view = viewFactory.create(container, this);

        vs.add(view);
        vs.attached();
    }

    createView() {...}
}

我这样用:

<menu-bar router.bind="router"></menu-bar>

不知何故this.router(如果我在MenuBar的构造函数中放置断点)总是null

提前致谢。

1 个答案:

答案 0 :(得分:2)

您需要在routerbind回调中设置任何依赖于attached的代码。 Aurelia绑定引擎没有机会绑定到构造函数中的router

组件生命周期回调列表位于:http://aurelia.io/docs.html#/aurelia/framework/1.0.0-beta.1.1.4/doc/article/creating-components/3