Aurelia以PHP传递的params开头 [英] Aurelia starting with params passed by PHP
问题描述
您可以使用普通JS访问的任何数据,您可以通过Aurelia访问。也许你可以使用 data - *
属性来做到这一点?当通过执行 aurelia-app =main来使用
host main
文件时,传递给configure方法的框架实例的属性是框架要连接的元素。您可以在此元素上放置
data - * 属性,然后通过此元素的
数据集属性(IE11 + https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement /数据集)。
您的 index.html
或同等物可能具有类似这样的内容:
< body aurelia-app =main
data-param1 =value1
data-param2 =value2>
您的 main.js
可以访问这些值很容易:
export function configure(aurelia){
aurelia.use
.standardConfiguration()
.developmentLogging();
aurelia.container.registerInstance('serverData',
Object.assign({},aurelia.host.dataset))
aurelia.start()。那么(()=> aurelia.setRoot());
}
这是一个可运行的示例: https://gist.run/?id=55eae2944b00b11357868262e095d28c
你甚至可以把JSON在数据属性中,如果围绕属性值使用单引号: https://gist.run/?id=57417139aa8c0c66b241c047efddf3dd
编辑:我根据Jeremy Danyow发布的类似答案改进了这个答案。这两个链接的要点也已更新。
I need to pas params to Aurelia on start. Depending on values passed the application will have diffrent state. This application is injected on page which is build with PHP, so the best way would be starting it with parameters specified with PHP code. Is there any way to do this?
Any data you can access in normal JS you can access with Aurelia. Maybe you could use a data-*
attribute to do this? When you use a main
file by doing 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` property of this element (IE11+ https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset).
Your index.html
or equivalent might have something like this:
<body aurelia-app="main"
data-param1="value1"
data-param2="value2">
Your main.js
can then access these values easily:
export function configure(aurelia) {
aurelia.use
.standardConfiguration()
.developmentLogging();
aurelia.container.registerInstance('serverData',
Object.assign({}, aurelia.host.dataset))
aurelia.start().then(() => aurelia.setRoot());
}
Here is a runnable example: https://gist.run/?id=55eae2944b00b11357868262e095d28c
You could even put JSON in the data attribute if you use single quotes around the attribute value: https://gist.run/?id=57417139aa8c0c66b241c047efddf3dd
Edit: I've improved this answer based on the similar answer Jeremy Danyow posted. Both linked gists have been updated as well.
这篇关于Aurelia以PHP传递的params开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!