Detailed explanation of routing and its usage in Angular

青灯夜游
Release: 2021-03-03 10:01:48
forward
1732 people have browsed it

This article takes you through routing in Angular and the use ofAngularrouting. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Detailed explanation of routing and its usage in Angular

Related recommendations: "angular Tutorial"

1. Angular creates a project with routing by default

1. Command to create a project

ng new ng-demo --skip-install

Detailed explanation of routing and its usage in Angular

2. Create the required components

ng g component components/home ng g component components/news ng g component components/newscontent
Copy after login

3. Find app-routing.module.ts and configure routing

Introduce components

import { HomeComponent } from './components/home/home.component'; import { NewsComponent } from './components/news/news.component'; import { ProductComponent } from './components/product/product.component';
Copy after login

Configure routing

const routes: Routes = [ {path: 'home', component: HomeComponent}, {path: 'news', component: NewsComponent}, {path:'product', component:ProductComponent }, {path: '*', redirectTo: '/home', pathMatch: 'full' } ];
Copy after login

4. Find app.component .html root component template, configure router-outlet to display dynamically loaded routes

首页 新闻

Copy after login

2. Angular routerLink jump page default route

首页 新闻
Copy after login
//匹配不到路由的时候加载的组件 或者跳转的路由 { path: '**', /*任意的路由*/ // component:HomeComponent redirectTo:'home' }
Copy after login

3. Angular routerLinkActive Set routerLink and select the route by default

首页 新闻

Copy after login

首页 新闻

Copy after login

4. Dynamic routing

4.1. Question mark parameter passing

jump Transfer method, page jump or js jump
The url address of the question mark parameter is displayed as.../list-item?id=1

queryParams attribute is fixed


{ { item.name }}

//js jump
//router is an instance of ActivatedRoute

import { Router } from '@angular/router'; . constructor(private router: Router) {} . this.router.navigate(['/newscontent'],{ queryParams:{ name:'laney', id:id }, skipLocationChange: true //可以不写,默认为false,设为true时路由跳转浏览器中的url会保持不变,传入的参数依然有效 });
Copy after login

How to get parameters

import { ActivatedRoute } from '@angular/router'; constructor(public route:ActivatedRoute) { } ngOnInit() { this.route.queryParams.subscribe((data)=>{ console.log(data); }) }
Copy after login

4.2 Path parameter passing

Path The URL address of the parameters is displayed as.../list-item/1

Path configuration:

{path: ‘list-item/:id’, component: ListItemComponent}
Copy after login

How to obtain parameters

this.route.params.subscribe( param => { this.id= param['id']; } )
Copy after login

5. Father and son Routing

1. Create component and introduce component

import { WelcomeComponent } from ‘./components/home/welcome/welcome.component’; import { SettingComponent } from ‘./components/home/setting/setting.component’;
Copy after login

2. Configure routing

{ path:'home', component:HomeComponent, children:[{ path:'welcome', component:WelcomeComponent },{ path:'setting', component:SettingComponent }, {path: '**', redirectTo: 'welcome'} ] },
Copy after login

3. Define router-outlet in parent component

Update For more programming related knowledge, please visit:programming video! !

The above is the detailed content of Detailed explanation of routing and its usage in Angular. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!