2016-10-13 42 views
2

我需要在開始時將參數傳遞給Aurelia。根據傳遞的值,應用程序將具有不同的狀態。該應用程序注入到使用PHP構建的頁面中,因此最好的方法是使用PHP代碼指定的參數啓動它。有沒有辦法做到這一點?Aurelia以params開頭通過PHP

回答

6

您可以在普通JS中訪問的任何數據,您可以通過Aurelia訪問。也許你可以使用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 data- * attributes on this element and then access them via the該元素的數據集屬性(IE11 + https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset)。

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

編輯:我改進了這個答案基於類似的答案傑里米Danyow發佈。兩個鏈接的要點也都進行了更新。

+1

類似的問題/方法:http://stackoverflow.com/a/36603297/725866 –

+0

嘿,我不知道!我通常在入口視圖(index.html或其他)中創建一個全局的地方,然後在Aurelia啓動時使用它。謝謝! –