Aurelia以PHP传递的params开头

时间:2016-10-13 14:40:22

标签: javascript php html startup aurelia

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

1 个答案:

答案 0 :(得分:6)

您可以使用Aurelia访问的普通JS中可以访问的任何数据。也许您可以使用data-*属性来执行此操作?通过执行此元素的main主机aurelia-app="main", the framework instance you get passed to your configure method has a数据 - * property that is the element the framework is being attached to. You could place数据集`属性(IE11 + https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset)来使用attributes on this element and then access them via the文件时。

您的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().then(() => aurelia.setRoot());  
}

这是一个可运行的示例:https://gist.run/?id=55eae2944b00b11357868262e095d28c

如果在属性值周围使用单引号,您甚至可以将JSON放在数据属性中:https://gist.run/?id=57417139aa8c0c66b241c047efddf3dd

编辑:我根据Jeremy Danyow发布的类似答案改进了这个答案。两个链接的要点也已更新。