javascript - Aurelia 模板引擎组成

标签 javascript aurelia templating

我正在尝试通过代码动态组合组件(而不是使用 <compose> )并将其从服务添加到 DOM(类似于弹出窗口)。

根据我的理解,最好的方法是使用 TemplatingEngine通过.compose方法。

我找不到关于如何准确使用它的像样的文档或小样本。

这就是我目前所拥有的

constructor(
    loggerFactory: LoggerFactory,
    private bindingEngine: BindingEngine,
    private templatingEngine: TemplatingEngine,
    private container: Container,
    private viewResources: ViewResources
) {

    const hostee = this.templatingEngine.compose({
        host: document.body,
        container: this.container,
        bindingContext: {

        },
        viewModel: SnackbarHostElement,
        viewResources: this.viewResources,
        viewSlot: new ViewSlot(document.body, true)
    });

    hostee.then(x => {
        x.attached();
    });

我没有收到任何错误,并且 .then正在被触发,但是该组件似乎没有渲染。任何帮助,将不胜感激!

最佳答案

这是最终的代码(感谢@Fabio)

async init(): Promise<View | Controller> {
    const viewController = await this.templatingEngine.compose({
        host: document.body,
        container: this.container,
        bindingContext: {

        },
        viewModel: this.container.get(SnackbarHostElement),
        viewResources: this.viewResources,
        viewSlot: new ViewSlot(document.body, true)
    });
    viewController.attached();
    return viewController;
}

关于javascript - Aurelia 模板引擎组成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42847544/

相关文章:

javascript - TypeError : _react2. 默认值。PropTypes 未定义

javascript - 在向 DOM 动态添加自定义元素后,如何让 Aurelia 呈现我的 View ?

javascript - 没有 eval() 就无法启动 Aurelia 应用程序

javascript - John Resigs 微型模板引擎,如何处理选择的问题?

javascript - 在 Javascript 中编写 HTML 的良好实践

javascript - 如何枚举 Javascript 中的错误对象?

javascript - 使用带有 getDay() 方法的 switch 语句?

javascript - 我们可以在 MooTools/Jquery 选择器中使用 OR 运算符吗

aurelia - 重复多个标签而不在Aurelia中创建容器

panel - 根据选定的模板变量隐藏 grafana 面板