Aurelia以PHP传递的params开头

Kub*_*uus 2 html javascript php startup aurelia

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

Ash*_*ant 6

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

index.html或同等人可能会有这样的事情:

  <body aurelia-app="main"  
        data-param1="value1" 
        data-param2="value2">
Run Code Online (Sandbox Code Playgroud)

main.js然后,您可以轻松访问这些值:

export function configure(aurelia) {
  aurelia.use
    .standardConfiguration()
    .developmentLogging();

  aurelia.container.registerInstance('serverData', 
    Object.assign({}, aurelia.host.dataset))

  aurelia.start().then(() => aurelia.setRoot());  
}
Run Code Online (Sandbox Code Playgroud)

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

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

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