我需要在开始时将参数传递给 Aurelia。根据传递的值,应用程序将具有不同的状态。该应用程序被注入到使用 PHP 构建的页面上,因此最好的方法是使用 PHP 代码指定的参数启动它。有什么办法可以做到这一点吗?
您可以在普通 JS 中访问的任何数据都可以通过 Aurelia 访问。也许你可以使用data-*
属性来做到这一点?当您使用main
通过做文件aurelia-app="main", the framework instance you get passed to your configure method has a
hostproperty that is the element the framework is being attached to. You could place
data-*attributes on this element and then access them via the
该元素的 dataset` 属性(IE11+https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset).
Your index.html
或同等内容可能有这样的内容:
<body aurelia-app="main"
data-param1="value1"
data-param2="value2">
Your main.js
然后可以轻松访问这些值:
export function configure(aurelia) {
aurelia.use
.standardConfiguration()
.developmentLogging();
aurelia.container.registerInstance('serverData',
Object.assign({}, aurelia.host.dataset))
aurelia.start().then(() => aurelia.setRoot());
}
这是一个可运行的示例:https://gist.run/?id=55eae2944b00b11357868262e095d28c https://gist.run/?id=55eae2944b00b11357868262e095d28c
如果您在属性值周围使用单引号,您甚至可以将 JSON 放入数据属性中:https://gist.run/?id=57417139aa8c0c66b241c047efddf3dd https://gist.run/?id=57417139aa8c0c66b241c047efddf3dd
编辑:我根据 Jeremy Danyow 发布的类似答案改进了这个答案。两个链接的要点也已更新。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)