FREELessons: 67Length: 8.9 hours

• Overview
• Transcript

# 2.1 Bootstrapping the Application

All Angular apps must be bootstrapped in order to load them in the target environment. In this lesson we take a look at the main entry point for our application, which is where this bootstrapping takes place.

## 10.Conclusion1 lesson, 01:32

### 2.1 Bootstrapping the Application

Hi folks. In this lesson we can take a look at how an Angular application is bootstrapped, which is like an initialization process whereby it is loaded in the browser. Angular apps don't have to be loaded in the browser. They can also run in NodeJS or other environments. When we do want to run an app in the browser, we need to Bootstrap it specifically for the browser. Because an Angular application is modular, it needs to have an entry point defined, which is where the module loader can load the initial module, which loads the rest of the application. The CLI has created an entry point for us. This is the main.ts file which is in the src directory. Let's open it up and take a look. At the top of the file, there are some import statements. These are interesting because they use the ES module syntax. The import and from keywords are pure JavaScript. Unfortunately, they aren't yet supported in all mainstream browsers, which is why we have to have Webpack in the project to load the modules for us. So the import keyword is followed by the identifier that we want to import within curly braces. Depending on the type of export that we're working with, we may or may not need the curly braces, but for most, maybe even all of the Angular imports, will import specific identifiers using this curly bracket syntax. We then use the from keyword to specify the path to the file that contains the identifier that we want to import. Webpack has some mappings in place, so we don't need to explicitly reference the node modules folder, which is where all of the Angular modules that we'll be using reside. After the Angular imports, we then have some application specific imports. We import the app module from the app.module file in the app folder, and the environment file from the src folder, which is the root of the application. After the imports, we then check whether we're running in production mode. At the moment, and for most of the time while we're developing, we'll be running in development mode and not production mode. Angular apps run in development mode by default, and if we want to run in production mode, we need to specifically tell Angular to do this. We can tell Angular to run in production mode using the CLI, and we'll take a look at this right at the end of the course. So when we do run a production build, environment.production will be true and we can signal to Angular to run in production mode by calling the global enable prod mode function. This was one of the things that we imported from the core Angular module at the top of the file. There are a number of differences between development mode and production mode. In production mode, our component templates will be pre-compiled. The generated JavaScript and CSS files will be concatenated and minified, and unused or dead code will be eliminated from the resullting package. None of these things are useful in development however, so they are only enabled in production mode. For loading the app in a browser, which is what we want to do most of the time, certainly in this course, we'll use the Bootstrap module method of the platform browser dynamic module. We pass in our own app module which is the root module of our application. This method returns a promise and has a catch method attached to handle any errors in the bootstrapping process. So this file already does everything that we need it to do. Even once we've completely built the application, this file won't need to change at all. Separating the booting of the application from the application itself like this is a great design. It means that we just don't need to worry about this file again. I just want to point out again, I did mention it already, but just so that we're clear, Angular uses the Webpack module loader under the hood to handle loading modules in the application. This is all configured for us automatically and we generally won't need to make any configuration changes to Webpack itself. So in this lesson, we saw that we can Bootstrap our application relatively easily using a couple of Angular modules. Remember, all bootstrapping is, is loading our root module. We also learned that we can handle running in production and development modes easily using the enableProdMode() method, which basically just handles everything for us. And that we used the bootstrapModule() method to bootstrap the app module. Thanks for watching.

Back to the top