Book Image

Angular Design Patterns

By : Mathieu Nayrolles
Book Image

Angular Design Patterns

By: Mathieu Nayrolles

Overview of this book

This book is an insightful journey through the most valuable design patterns, and it will provide clear guidance on how to use them effectively in Angular. You will explore some of the best ways to work with Angular and how to use it to meet the stability and performance required in today's web development world. You’ll get to know some Angular best practices to improve your productivity and the code base of your application. We will take you on a journey through Angular designs for the real world, using a combination of case studies, design patterns to follow, and anti-patterns to avoid. By the end of the book, you will understand the various features of Angular, and will be able to apply well-known, industry-proven design patterns in your work.
Table of Contents (9 chapters)

Environment setup

For the environment setup, I will cover all three major platforms: Debian-flavored Linux, macOS, and Windows. All the tools we are going to use are cross-platform. Consequently, feel free to choose the one you like the most; there is not a thing you will not be able to do later on.

In what follows, we will install Node.js, npm, and TypeScript.

Node.js and npm for Linux

$ curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
$ sudo apt-get install -y Node.js

This command downloads a script, directly into your bash, that will fetch every resource you need and install it. For most cases, it will work just fine and install Node.js + npm.

Now, this script has one flaw; it will fail if you have Debian repositories that are no longer available. You can either take this opportunity to clean your Debian repositories or edit the script a bit.

$ curl https://deb.nodesource.com/setup_6.x > node.sh 
$ sudo chmod +x node.sh 
$ vim node.sh 
//Comment out all apt-get update 
//Save the file 
$ sudo apt-get update 
$ ./node.sh 
$ sudo apt-get update 
$ sudo apt-get install -y Node.js 

Then, go to https://Node.js.org/en/download/, and download and install the last .pkg or .msi (for Linux or Windows, respectively).

TypeScript

Now, you should have access to node and npm in your Terminal. You can test them out with the following commands:

$ node -v 
V8.9.0 
 
$ npm -v 
5.5.1 
 

Note that the output of these commands (for example, v6.2.1 and 3.9.3) can be different, and your environment as the latest version of node and npm can, and most certainly, will be different by the time you read these lines. However, if you at least have these versions, you will be fine for the rest of this book:

    $ npm install -g TypeScript

The -g argument stands for global. In the Linux system, depending on your distribution, you might need sudo rights to install global packages.

Very much like node and npm, we can test whether the installation went well with the following:

    $ tsc -v
    Version 2.6.1
  

What we have, for now, is the TypeScript transpiler. You can use it like so:

    tsc --out myTranspiledFile.js myTypeScriptFile.ts
  

This command will transpile the content of myTypeScriptFile.ts and create myTranspiledFile.js. Then, you can execute the resultant js file, in the console, using node:

    node myTranspiledFile.js
  

To speed up our development process, we will install ts-node. This node package will transpile TypeScript files into JavaScript and resolve the dependencies between said files:

    $ npm install -g ts-node
    $ ts-node -v
    3.3.0

Create a file named hello.ts and add the following to it:

console.log('Hello World'); 

Now, we can use our new package:

    $ ts-node hello.ts 
    Hello World