官术网_书友最值得收藏!

The Bootstrap process

Like many JavaScript frameworks such as Angular and React, Aurelia needs a place in the index.html page to mount the application. This place is known as the entry point. Open the index.html file, and you should see something similar to the following code:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Aurelia</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body aurelia-app="main">
<script src="scripts/vendor-bundle.js" data-main="aurelia-bootstrapper"></script>
</body>
</html>

Aurelia requires an HTML element to load our application. By default, the application is loaded in the body element; we know this because this element uses the aurelia-app attribute, which is used to specify the main JavaScript script file that contains all the configuration for our application, and as you note, by default, Aurelia is configured to use the main file. The following is the content of the main.js file:

import environment from './environment';

export function configure(aurelia) {
aurelia.use
.standardConfiguration()
.feature('resources');

if (environment.debug) {
aurelia.use.developmentLogging();
}

if (environment.testing) {
aurelia.use.plugin('aurelia-testing');
}

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

Let's analyze this file; the first line imports the environment variables from the environment.js file located in the root folder. When you specify the --flag {env} option, the CLI looks for the {env}.json file in the aurelia_project folder and copies its content into the environment.js file.

This file also exports a single configure function, which receives as a parameter the aurelia object that you use to override default configurations and add any code you wish before the app is launched. For example, you can tell Aurelia that you want to declare components as global (features), configure internationalization to manage different languages, and so on.

Once the aurelia object is configured, the last line of code will render our application into the root HTML element, which has the aurelia-app attribute in the index.html page. By default, it renders the app.js component into the root element. Of course, we can override the default values by passing the element you wish to render as the first parameter and the HTML element where you wish to render the app as a second parameter:

 aurelia.start().then(() => aurelia.setRoot('my-component', document.getElementById('my-p')));

We will be modifying this file along the way; the most important thing to remember is that this file is processed before the app is rendered and apart from the Aurelia.json file, this is the second most important file. The following diagram explains the bootstrapping process:

Now you know how the bootstrapping process works. Let's understand how you can create reusable components.

主站蜘蛛池模板: 田东县| 台山市| 德阳市| 化德县| 丽水市| 绥滨县| 清河县| 金溪县| 沛县| 呼图壁县| 车致| 九江市| 农安县| 容城县| 三台县| 晋中市| 双牌县| 芷江| 南澳县| 普定县| 太原市| 卫辉市| 石棉县| 疏勒县| 阳原县| 台州市| 牡丹江市| 华亭县| 崇州市| 兴隆县| 广德县| 正阳县| 乐业县| 威远县| 萨迦县| 酒泉市| 马山县| 梓潼县| 旺苍县| 修水县| 连云港市|