Detailed explanation of Angular routing basics

Detailed explanation of Angular routing basics

1. Routing related objects

Router and RouterLink have the same function, which is navigation. Router is used in Controller, and RouterLink is used in template.

2. Location of routing objects

1. Routes object

Configuration is in the module. Routes consists of a set of configuration information, each of which contains at least two attributes, Path and Component.

2. RouterOutlet

In the template

3.RouterLink

Instructions to generate links in templates and change URLs

4. Router

In the Controller, call the navigate method of the Router object to switch routes.

5. ActivatedRoute

When routing, data is passed through the URL and the data is saved in the ActivatedRoute object.

3. Routing Configuration

When using the ng new --routing parameter, an additional app-routing.module.ts file will be generated

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

const routes: Routes = [];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

Will automatically import into app.module.ts.

Generate two components: home component and component component.

const routes: Routes = [
  {path: '', component : HomeComponent}, //path is empty{path: 'product', component: ProductComponent}
];

Notice:

1. The path configuration cannot start with a slash and cannot be configured as path:'/product'.

Because the Angular router parses and generates URLs, not starting with / allows you to use relative and absolute paths freely when navigating between multiple views.

2. When writing a path in a template, it must start with /.

Because using a slash plus a . indicates whether you want to navigate to the root route (/) or the sub-route (./).

/ means navigating to the root route and searching from the layer where the root route is configured.

<a [routerLink]="['/']">Home</a>

3. Display component content under <router-outlet>

4. The routerLink parameter is an array instead of a string

Because parameters can be passed during routing.

4. Navigating through the Router object in the code

Add a button to the template

<input type="button" value="Product Details" (click)="toProductDetails()">

Use router.navigate to navigate in the controller.

The navigate parameter is configured the same as the routerLink parameter.

import { Component } from '@angular/core';
import { Router } from '@angular/router';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  constructor(private router:Router){
  }
  toProductDetails(){
    this.router.navigate(['/product']);
  }
}

Clicking a button has the same effect as clicking a link.

5. Configuring a non-existent path

Generate code 404 component to show that the page does not exist.

The first matched route takes precedence, so the ** wildcard route should be placed last.

const routes: Routes = [
  { path: '', component: HomeComponent }, // path is empty { path: 'product', component: ProductComponent },
  { path: '**', component: Code404Component }
]; 

6. Redirection Routing

Redirects an address to another specified component

www.aaa.com => www.aaa.com/products

www.aaa.com/x => www.aaa.com/y The user may have bookmarked the x address.

Using redirect routing

const routes: Routes = [
  { path: '', redirectTo : 'home', pathMatch:'full' }, //The path is empty { path: 'home', component: HomeComponent },
  { path: 'product', component: ProductComponent },
  { path: '**', component: Code404Component }
];

7. Passing data during routing

There are 3 ways

1. Passing data in query parameters

2. Passing data in the routing path

When defining the routing path, you need to specify the parameter name and carry the parameter in the actual path.

3. Passing data in routing configuration

The above is a detailed explanation of the basics of Angular routing. For more information about the basics of Angular routing, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Detailed explanation of Angular routing sub-routes
  • Detailed explanation of Angular routing animation and advanced animation functions
  • Angular multi-level routing to achieve login page jump (novice tutorial)
  • Usage of default routing in angular

<<:  How to backup MySQL regularly and upload it to Qiniu

>>:  Windows CVE-2019-0708 Remote Desktop Code Execution Vulnerability Reproduction Issue

Recommend

Mini Program to implement Token generation and verification

Table of contents process Demo Mini Program Backe...

Pitfall notes of vuex and pinia in vue3

Table of contents introduce Installation and Usag...

MySQL variable principles and application examples

In the MySQL documentation, MySQL variables can b...

Solution to Docker's failure to release ports

Today I encountered a very strange situation. Aft...

Docker /var/lib/docker/aufs/mnt directory cleaning method

The company's service uses docker, and the di...

CSS controls the spacing between words through the letter-spacing property

letter-spacing property : Increase or decrease th...

Essential bonus items for optimizing and packaging the front end of Vue projects

Table of contents Preface 1. Routing lazy loading...

How to implement a password strength detector in react

Table of contents Preface use Component Writing D...

Detailed explanation of the use of Linux seq command

01. Command Overview The seq command is used to g...

Solution to the garbled code problem in MySQL 5.x

MySQL is a commonly used open source database sof...

Detailed tutorial on using Docker to build Gitlab based on CentOS8 system

Table of contents 1. Install Docker 2. Install Gi...

JavaScript to implement click to switch verification code and verification

This article shares the specific code of JavaScri...

Using JS timer to move elements

Use JS timer to make an element to make a method ...

Setting up a proxy server using nginx

Nginx can use its reverse proxy function to imple...

How to modify Flash SWF files in web pages

I think this is a problem that many people have en...