Detailed explanation of Angular routing sub-routes

Detailed explanation of Angular routing sub-routes

1. Sub-route syntax

2. Examples

On the product details page, in addition to the product ID information, it also displays the product description and the salesperson's information.

The product description component and salesperson information component are displayed inside the product details component through sub-routing.

1. Create 2 new components and modify their contents

ng g component productDesc
ng g component sellerInfo

The key point is to modify the salesperson information component to display the salesperson ID.

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-seller-info',
  templateUrl: './seller-info.component.html',
  styleUrls: ['./seller-info.component.css']
})
export class SellerInfoComponent implements OnInit {
  private sellerId: number;
  constructor(private routeInfo: ActivatedRoute) { }

  ngOnInit() {
    this.sellerId = this.routeInfo.snapshot.params["id"];
  }

}

2. Modify the routing configuration

Add sub-routes to the product component

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

3. Modify the template of product.component.ts

Note: routerLink must be configured as ./ and / cannot be used again.

<p>
  This is the product information component</p>
<p>
  The product id is: {{productId}}
</p>

<a [routerLink]="['./']">Product Description</a>
<a [routerLink]="['./seller',99]">Salesperson information</a>
<router-outlet></router-outlet>

Effect:

The main route is /product/2, and the sub-route is an empty string:

The product details component of the main route is displayed, and the product description component corresponding to the empty string of the sub-route is also displayed.

Click the salesperson information link:

The URL path becomes: http://localhost:4201/product/2/seller/99.

The sub-route seller/99 and the corresponding sellerInfo component are also displayed.

Notice:

1. The socket router-out forms a parent-child relationship and can be nested infinitely

2. All routing information is configured at the module level in app.routing.module.ts.

Routing information is at the module level, and all components themselves do not know any information related to routing.

Parent-child relationship between sockets - child routing.

Sibling relationship between sockets - auxiliary routing.

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

You may also be interested in:
  • Detailed explanation of Angular routing basics
  • 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

<<:  Detailed explanation of MySQL locks (table locks, row locks, shared locks, exclusive locks, gap locks)

>>:  Summary of Nginx load balancing methods

Recommend

MySQL 5.7.27 installation and configuration method graphic tutorial

MySQL 5.7.27 detailed download, installation and ...

A brief discussion on the issue of element dragging and sorting in table

Recently, when using element table, I often encou...

JavaScript+HTML to implement student information management system

Table of contents 1. Introduction 2. Rendering 3....

js implements clock component based on canvas

Canvas has always been an indispensable tag eleme...

In-depth understanding of MySQL long transactions

Preface: This article mainly introduces the conte...

Summary of Linux system user management commands

User and Group Management 1. Basic concepts of us...

How to view server hardware information in Linux

Hi, everyone; today is Double 12, have you done a...

Detailed steps for configuring virtual hosts in nginx

Virtual hosts use special software and hardware t...

MySQL 8.0.18 installation and configuration method graphic tutorial (linux)

This article records the installation and configu...

Example of how to identify the user using a linux Bash script

It is often necessary to run commands with sudo i...

Summary of practical skills commonly used in Vue projects

Table of contents Preface 1. Use $attrs and $list...

React introduces antd-mobile+postcss to build mobile terminal

Install antd-mobile Global import npm install ant...

MySQL table and column comments summary

Just like code, you can add comments to tables an...

Detailed explanation of Docker daemon security configuration items

Table of contents 1. Test environment 1.1 Install...

One line of code teaches you how to hide Linux processes

Friends always ask me how to hide Linux processes...