Angular 2+: What are the Best Practices & Tips When Using One of the Best Modern Frameworks?
Tags: angular, webdev
Published: 2019-10-04 13:29:18

##Intro
<p align="justify">Nowadays the modern frameworks provide a lot of instruments to make life of developers much easier. The frameworks are aiming to simplify the code writing process and consequent maintenance of it. Nevertheless, the wide range of frameworks' options may affect the quality of the code, especially if it is a large scalable project. The reason of low-quality code might be the neglection of the guidelines for working with a particular framework. In other words, neglection to follow the "style guide".</p> 

<p align="justify">In fact, Angular is considered as one of the most powerful JavaScript frameworks for front-end development, but simultaneously, it is not a simple framework to use. This article, we will focus on the best practices of modern Angular development, with which high-quality web applications are created.</p>


## 1. Features recommendations

#### AngularCLI 
<p align="justify">Speaking about the modern usage of Angular, the <u><a href="https://angular.io/cli" rel="nofollow">Angular CLI</a></u> is the one that need to be mentioned. This tool allows to improve the efficiency of working with the project for every developer. The possibilities of Angular CLI encompass the creation of a new project, the adjustment of its parameters, the extention of new features, serving and much more. To use these features it is necessary to install Node.js v8.9 or higher and NPM v5.5.1. Next, you should install Angular CLI to the system globally with the next command:</p>

> **npm install -g @angular/cli**

<p align="justify">After that, Angular CLI will be available to use. Here is an example of commands for creating and running a development server for the new Angular application:</p>

> **ng new new-project-name
> cd new-project-name
> ng serve**

<p align="justify">Check out the <u><a href="https://cli.angular.io/" rel="nofollow">Angular CLI website</a></u> in order to get additional information about techniques, tutorials and much more.</p>

#### SASS 
<p align="justify">Another best practice of Angular development is using <u><a href="https://sass-lang.com/" rel="nofollow">SASS</a></u> for components styling. Firstly, there is a lot of well-known benefits of using it: variables, mixins, functions, etc. Secondly, SASS preprocessor is the default for many popular 3rd-party-libraries for Angular, such as Material Components, which also used to manage the themes of the design. By default, when a new project is created using Angular CLI, it sets CSS for styling. </p>
<p align="justify">To set SASS as default for it you need to run the next command:</p>

> **ng new new-project-name --style=scss**

#### Angular Dependency Injection
<p align="justify">When Angular 7 came, it brought a new way to provide injectable services. Earlier services have been provided in <i>NgModule only (preferably in the Core module)</i>. Now, when a new service is generated with Angular CLI, it is automatically provided in the root module, using "provideIn" key of <i>@Injectable</i> decorator.</p>

```javascript
@Injectable({
  	providedIn: 'root'
})
```

<p align="justify"><b>"Root"</b> is the default value which was sat by Angular CLI and could be replaced with any NgModule of application.</p>

#### RxJs 
<p align="justify"><u><a href="https://rxjs-dev.firebaseapp.com/" rel="nofollow">RxJs</u></a> is tricky to learn and to understand. But Angular widely uses it. In order to apply the methods of reactive programming successfully, the developer should start thinking in terms of streams, not actions. Furthermore, you have to study the basic capabilities of RxJs, so you might see all the advantages of the reactive approach and simulteniously understand why this library plays such a significant role in the Angular framework.</p>

<p align="justify">Briefly, it uses an observer pattern that involves the presence of a certain subject and a number of observers. The observers subscribe to the subject events (change of state), and when it happens, they respond by calling their methods. Ultimately, RxJs makes it possible to create a more readable and maintainable code.</p>


## 2. Project structure recommendations

<p align="justify">For best maintenance, Angular projects have a typical project structure, components naming and must comply with the uniform rules. These rules have been written in details by Angular Team in <u><a href="https://angular.io/guide/styleguide" rel="nofollow">the Style Guide section</a></u>. They affect both the names of components, services, resolvers, directives, pipes, and their joint organization within a single project. Let us take a look at the most useful recommendations for project organization.</p>

<p align="justify">First of all, we have to create a correct structure of <b>Angular modules</b>. In the style guide, we can read about the app, shared, core and feature modules.</p>

<p align="justify"><b>App module</b> - the main (root) module of every Angular application. The existence of this module is defined by the philosophy of Angular, which says, the application has to have at least one root module.</p>

<p align="justify"><b>Core module</b> - another significant module of the Angular application which is used for importing 3rd-party-libraries, providing singleton services, etc. In general, the Core modules should make the App module more readable, taking over part of the functional.</p>

<p align="justify">The purpose of the <b>Shared module</b> is for import and re-exporting reusable components, pipes, directives, and Angular modules, which could be used many times. Using it, you can get rid of the need to import a number of modules into the module of some features, importing instead only one Shared module.</p>

<p align="justify">Also, the Angular team recommends creating its own <b>feature modules</b> for each independent feature. This makes the project easily supported, allows you to formalize the structure of components more accurately and to make the component available for lazy loading.</p>

## 3. Performance recommendations

<p align="justify">Angular since version 2+ gives developers a wonderful opportunity to use lazy loading modules. Its mechanism involves downloading the component modules as they become necessary as a result of the user's routing navigation. Using lazy loading allows developers to minimize the size of the originally downloaded application, limiting them to only those components, that are used on the page which is loaded. All other components that are available by routing will be lazily loaded after navigating the corresponding routing. In cases with large scalable projects, this approach gives a significant gain in application performance and minifies download time.</p>

<p align="justify">Below there is an example of a code that describes the routing configuration with a lazy-loaded Products module.</p>

```javascript
const routes: Routes = [
  	{
    		path: 'products',
    		loadChildren: 'app/products/products.module#ProductsModule'
 	 }
];
```

<p align="justify">Another way to get the best performance of the Angular application is production build. Angular CLI only generates a development build script (with "ng build" script) when generating a new project by default. To get more productive build we have to add to <i>package.json</i> next script:</p>

```javascript
"build:prod": "ng build --prod --build-optimizer --vendor-chunk" 
```

<p align="justify">These flags modify a lot of build options for optimizing output build. Check out <u><a href="https://angular.io/cli/build" rel="nofollow"><i>ng build</i> page</a></u> for more information regarding this issue.</p>

## Conclusion
<p align="justify"> As you can see Angular framework is one of the most powerful JavaScript frameworks for front-end development, therefore knowing the tips and tricks regarding its implementation will help you to do high-quality work at the end. The article was focusing on precise recommendations that we hope you find useful. 

Liked that? We’ve done our best! Go to our <u><a href="https://2muchcoffee.com/blog/"> blog </a></u>to find more useful articles.