Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Angular Projects

You're reading from   Angular Projects Build modern web apps by exploring Angular 12 with 10 different projects and cutting-edge technologies

Arrow left icon
Product type Paperback
Published in Jul 2021
Publisher Packt
ISBN-13 9781800205260
Length 344 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Aristeidis Bampakos Aristeidis Bampakos
Author Profile Icon Aristeidis Bampakos
Aristeidis Bampakos
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Chapter 1: Creating Your First Web Application in Angular 2. Chapter 2: Building an SPA Application with Scully and Angular Router FREE CHAPTER 3. Chapter 3: Building an Issue Tracking System using Reactive Forms 4. Chapter 4: Building a PWA Weather Application Using Angular Service Worker 5. Chapter 5: Building a WYSIWYG Editor for the Desktop using Electron 6. Chapter 6: Building a Mobile Photo Geotagging Application Using Capacitor and 3D Maps 7. Chapter 7: Building an SSR Application for a GitHub Portfolio Using Angular 8. Chapter 8: Building an Enterprise Portal Using Nx Monorepo Tools and NgRx 9. Chapter 9: Building a Component UI Library Using Angular CLI and Angular CDK 10. Chapter 10: Customizing Angular CLI Commands Using Schematics 11. Other Books You May Enjoy

Setting up routing in an Angular application

We will kick off our project by creating a new Angular application from scratch. Execute the following command of the Angular CLI in a terminal window to create a new Angular application:

ng new my-blog --routing --style=scss

We use the ng new command to create a new Angular application, passing the following options:

  • my-blog: The name of the Angular application that we want to create. The Angular CLI will create a my-blog folder in the path where we execute the command.

    Important Note

    Every command that we run in the terminal window should be run inside this folder.

  • --routing: Enables routing in the Angular application.
  • --style=scss: Configures the Angular application to use the SCSS stylesheet format when working with CSS styles.

When we enable routing in an Angular application, the Angular CLI imports several artifacts from the @angular/router npm package in our application:

  • It creates the app-routing.module.ts file, which is the main routing module of our application:
    import { NgModule } from '@angular/core';
    import { RouterModule, Routes } from '@angular/router';
    const routes: Routes = [];
    @NgModule({
      imports: [RouterModule.forRoot(routes)],
      exports: [RouterModule]
    })
    export class AppRoutingModule { }
  • It imports AppRoutingModule into the main module of our application, app.module.ts:
    import { NgModule } from '@angular/core';
    import { BrowserModule } from '@angular/platform-browser';
    import { AppRoutingModule } from './app-routing.module';
    import { AppComponent } from './app.component';
    @NgModule({
      declarations: [
        AppComponent
      ],
      imports: [
        BrowserModule,
        AppRoutingModule
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    export class AppModule { }

We configured our application to use the SCSS stylesheet format. Instead of creating the styles of our application manually, we will use the Bootstrap CSS library:

  1. Execute the following command in a terminal window to install Bootstrap:
    npm install bootstrap

    We use the npm executable to install the bootstrap package from the npm registry in the previous command.

  2. Add the following import statement at the top of the styles.scss file that exists in the src folder of our Angular application:
    @import "~bootstrap/scss/bootstrap";

    The styles.scss file contains CSS styles that are applied globally in our application. In the previous snippet, we import all the styles from the Bootstrap library into our application. The @import CSS rule accepts the absolute path of the bootstrap.scss file as an option, without adding the .scss extension. The ~ character represents the node_modules folder of our Angular application.

    Important Note

    The node_modules folder contains all the npm packages and libraries that our application needs, either during development or runtime.

In the following section, we will learn how to create the basic layout of our blog by creating components, such as the header and the footer.

You have been reading a chapter from
Angular Projects - Second Edition
Published in: Jul 2021
Publisher: Packt
ISBN-13: 9781800205260
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime