Aurelia以PHP传递的params开头 [英] Aurelia starting with params passed by PHP

查看:106
本文介绍了Aurelia以PHP传递的params开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在开始时将参数传递给Aurelia。根据传递的值,应用程序将具有不同的状态。该应用程序注入到使用PHP构建的页面中,因此最好的方法是使用PHP代码指定的参数启动它。有没有办法做到这一点?

解决方案

您可以使用普通JS访问的任何数据,您可以通过Aurelia访问。也许你可以使用 data - * 属性来做到这一点?当通过执行 aurelia-app =main来使用 main 文件时,传递给configure方法的框架实例的 host 属性是框架要连接的元素。您可以在此元素上放置 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 ahostproperty that is the element the framework is being attached to. You could placedata-*attributes on this element and then access them via thedataset` 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆