Aurelia - 配置

在本章中,我们将向您展示如何根据您的需求配置Aurelia框架.有时您需要在将应用呈现给用户之前设置初始配置或运行一些代码.

步骤1  - 创建main.js

让我们在 src 文件夹中创建 main.js 文件.在这个文件中我们将配置Aurelia.

您还需要告诉Aurelia加载配置模块.您可以在以下示例中看到注释部分.

index.html

<!DOCTYPE html>
<html>
   <head>
      <title>Aurelia</title>
      <link rel = "stylesheet" href = "styles/styles.css">
      <meta name = "viewport" content = "width=device-width, initial-scale = 1">
   </head>

   <body aurelia-app = "main"> 
      <!--Add "main" value to "aurelia-app" attribute... -->
      <script src = "jspm_packages/system.js"></script>
      <script src = "config.js"></script>
		
      <script>
         SystemJS.import('aurelia-bootstrapper');
      </script>
   </body>
</html>

步骤2  - 默认配置

下面的代码显示了如何使用默认配置. configure 功能允许手动设置配置.我们设置使用属性来指定我们需要的内容.

main.js

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

   aurelia.start().then(() => aurelia.setRoot());
}

第3步 - 高级配置

我们可以使用很多配置选项.为了向您展示所有内容超出了本文的范围,因此我们将在以下示例中解释配置的工作原理.我们基本上告诉Aurelia使用默认数据绑定语言,默认资源,开发日志记录,路由器,历史记录事件聚合器.这些是标准的插件集.

export function configure(aurelia) {
   aurelia.use
   .defaultBindingLanguage()
   .defaultResources()
   .developmentLogging()
   .router()
   .history()
   .eventAggregator();

   aurelia.start().then(() => aurelia.setRoot());
}

注意 : 这些设置将在下一章中详细说明.