Getting Started with Angular CLI
In this article, we will explore the different commands and features of the Angular CLI.Angular is one of the most popular frameworks for web app development at the moment. Over the years, it has grown into a mature framework, and now offers a host of features to supercharge your dev process. 🚀 A lot of new Angular developers talk about a steep learning curve when learning the framework. This could be why: If you're new to Angular, you'll have your hands full trying to grasp all these new concepts and the terminology around them. To make things easier when setting up a project and scaffolding new pieces, the Angular team has built Angular CLI . Angular CLI is the Command Line Interface for creating, building and scaffolding new functionality for Angular applications. It provides all the commands you need to rapidly spin up new Angular applications - starting from the build process, all the way to unit tests, end to end tests, and even deployment. You can then run npm i -g @angular/cli to install the Angular CLI. Angular CLI allows you to scaffold a full project structure from scratch, with dependencies and build tooling preinstalled and configured. You can use this command to set it up. When you run this command, the Angular CLI asks you a couple of questions so it can set things up correctly. Once you choose your preferred options, the following folder structure is created: This is a pretty standard NPM-based project which uses TypeScript and webpack . Since it is a single page application, it only contains one index.html file. The webpack configuration is abstracted away for simplicity, and all the configurable parameters are set in the angular.json file. The project uses karma and jasmine for unit testing and protractor for end-to-end testing. It comes with a single module pre-created, with an AppComponent which is the entry point for anyone using the app. We will learn more about modules, components and other types of files in the upcoming sections. An Angular module or NgModule is a container for components, services, pipes and any other functionality. Think of it as a way to group your application features or pages. Modules can export functionality that's declared within them, and can also import other modules. In theory, you could just use the one AppModule that's pre-created when you generate the project, but as your application grows, you will find it useful to logically group functionality to facilitate advanced features such as lazy loading. You can generate Angular modules using the command or Here's what a typical module file looks like: The @NgModule declaration contains the following properties: An Angular component is a unit of functionality that comprises of 4 files: There is also a .spec.ts file included, which will contain tests for your functionality. You can generate Angular components using the command or When you run the ng generate component command, these files are generated, and the nearest module is updated to include the new component in its declarations array. Angular allows for dependency injection using the concept of providers . Any class that includes the @Injectable decorator is considered a provider. It can be injected into a component, service or pipe through its constructor. You can generate an injectable service using the command or Consider the following example: The @Injectable decorator here specifies that this class PizzaService is provided in the root module. This is a more recent way of configuring a provider. Another way is to include it in the providers array for the required NgModule . If PizzaService were to require functionality from another service, it could be injected through its constructor like so: Services are generally used to provide shared functionality such as making HTTP requests, transforming data etc. Angular includes its own HTTP client which is based on RxJS observables. This is quite different from traditional Promise-based implementations and can be a bit hard to wrap your head around. This article is a great resource that explains these concepts. Services are instantiated once for each module they are provided in. So if a service was provided in the root module, it would only be instantiated once for your entire application. An Angular pipe is a special function that takes a value, applies some transformation and returns the transformed value. You can generate a pipe using the command or When a pipe is generated, the nearest module's declarations array is updated to include the new pipe. Here's an example: The ShoutPipe transforms any input into uppercase. This pipe can be instantiated in two ways: Angular CLI abstracts away all the build-related setup and configuration and provides a few commands that perform all these functions. The ng build command compiles your application as per the configuration specified in angular.json and tsconfig.json . It generates a dist folder with the build output. This command takes several command-line arguments . One of the most used variants is ng build --prod which creates a production-ready build of your app. This applies optimisations such as minification, tree shaking and dead code elimination to reduce the overall bundle size When developing your application locally, the Angular CLI provides a means of spinning up a local web server using the ng serve command. This command makes use of Webpack Dev Server under the hood. It watches for any changes in your source files and automatically reloads your browser tab(s). Angular CLI projects come with Karma and Jasmine pre-installed and configured. Karma is the test runner, and Jasmine is the testing framework. There are also Angular APIs available for creating test modules and mocking out dependencies. You can run any tests you've written using this command. This will run the tests in watch mode, which means they will be re-run whenever the source files change. You can run ng test with the argument --watch=false in case you'd like to do a single run e.g. in your CI environment. Angular CLI includes TSLint tooling to enforce code style and detect violations. It provides an opinionated set of rules, which can be overridden if required by modifying the tslint.json file. You can run this command to detect code style violations in your source code. This file contains all the configuration for how your Angular workspace is organized, built and served. A workspace can contain multiple projects, and they are all included in the projects object. Each project contains an architect section, where the builders and configuration for each command are defined. The example file below uses the default builders specified by the CLI. Custom builders can be installed as NPM dependencies and can be specified in place of the default ones. You can also write custom builders as per the guide available here . This article from Angular In Depth is also a great resource for understanding how builders work. Each project also has a schematics property, which can be used to specify extensions to Angular CLI functionality. There are different types of schematics: Schematics are a relatively recent addition to the Angular ecosystem. More information on how they work and how to use them can be found here and here . In this article, we have learnt about what Angular is, and the reasons why new Angular developers perceive it to have a steep learning curve. We have looked at the toolchain that Angular CLI provides and the different commands in-depth. During the process, we have also explored most of the important concepts of the Angular framework. We have seen how Angular CLI's tooling and ecosystem can abstract away the boring and fiddly parts of web development, and allow you, as an application developer, to focus on developing your application. Nx is a great set of tools that extends the functionality of the Angular CLI. This article explains how to set it up for your workspace. There are a number of great articles on Newline about specific Angular-related topics. The documentation available on the Angular website is top-notch and should answer most questions you may have. https://indepth.dev/ (previously Angular in Depth) is a great resource for articles, blogs and tutorials about Angular. https://blog.angular.io/ has articles written by Angular team members. It is generally the first place to feature new announcements about the framework.