【发布时间】:2017-12-30 15:26:54
【问题描述】:
我们将所有页面内容和博客文章等存储在 WordPress 中,使用它的 API 在我们的 Aurelia 应用程序中呈现数据。这很好用;
<div class="excerpt" innerhtml.bind="post.excerpt.rendered"></div>
作者现在希望能够链接到弹出窗口或在其博客文章中使用 route-href 或其他自定义 Aurelia 属性,但是使用 innerhtml.bind 添加到页面的代码不会被 Aurelia 解析。
我喜欢普通的 <a href="..."> 在 Aurelia 中“正常工作” - 但我们有很多自定义属性(例如作者无法使用的 <button popup="name-of-popup">...</button>。
我们如何克服这个问题?
编辑:使用来自@bigopon 的 cmets,我已经开始做一些事情,但仍然无法让它发挥作用。要么我不擅长搜索,要么TemplatingEngine.enhance() 方法的文档有点缺乏,但我尝试创建一个这样的自定义属性:
import {Aurelia, inject, TemplatingEngine} from 'aurelia-framework';
@inject(Element, Aurelia, TemplatingEngine)
export class AureliaEnhanceCustomAttribute {
constructor (el, aurelia, templatingEngine) {
this.el = el;
this.aurelia = aurelia; // NOTE: I've never done this before - is it even correct?
this.templatingEngine = templatingEngine;
}
attached () {
this.el.innerHTML = this.value;
// NOTE: Without checking for this we get an endless loop of attached() calls
if (!this.el.classList.contains('au-target')) {
this.templatingEngine.enhance({
element: this.el,
container: Aurelia.container, // NOTE: I'm guessing here
resources: Aurelia.resources, // NOTE: Same here, but I need all my global resources to be available inside the enhanced element too
bindingContext: {} // NOTE: Not sure what to pass in here at all.. :/
});
}
}
}
我就是这样使用它的:
<div aurelia-enhance.bind="exampleContent"></div>
exampleContent 是从 API 调用中提取的字符串,可能类似于:'<my-custom-element></my-custom-element><button my-custom-attribute="some-value">Log in</button>'。
【问题讨论】:
-
使用 innerhtml 时,“解析”是什么意思?
-
如果作者在他们的博客文章或页面中使用自定义属性或元素,它们不会被 Aurelia“解析”(也许是错误的词?),因此元素或属性永远不会运行。
-
你需要的不是
innerhtml,使用TemplatingEnginecompose/enhance函数,会有帮助的 -
好的,你有机会用一个例子发布答案吗?
-
这个周末或者今天晚些时候我应该可以写一篇了。如果对您来说很紧急,请查看 aurelia 对话框或在 aurelia 模板资源 repo 中撰写元素。
标签: javascript aurelia