Angular【router路由】

基本用法

生成路由模块文件

shell 复制代码
ng generate module app-routing --flat --module=app
typescript 复制代码
// app-routing.module.ts`
import { NgModule }             from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { FooComponent } from './foo/foo.component'
import { BarComponent } from './bar/bar.component'

const routes: Routes = [ // 定义路由
  {
    path: 'foo',
    component: FooComponent
  },
  {
    path: 'bar',
    component: BarComponent
  }
]
@NgModule({
  imports: [
    RouterModule.forRoot(routes)
  ],
  exports: [ RouterModule ]
})
export class AppRoutingModule {}
  • path不能以 / 开头

设置路由出口

html 复制代码
<h1>{{title}}</h1>
<router-outlet></router-outlet>

重定向

复制代码
{ path: '', redirectTo: '/dashboard', pathMatch: 'full' },

路由跳转

标签跳转

RouterLink 指令是 Angular 声明式导航的方法。它允许你使用标准的锚点元素 (<a>),这些元素可以与 Angular 的路由系统无缝集成。

html 复制代码
<ul>
  <li>
    <a routerLink="/foo">Go Foo</a>
  </li>
  <li>
    <a routerLink="/bar">Go Foo</a>
  </li>
</ul>

编程跳转

ts 复制代码
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router'

@Component({
  selector: 'app-user',
  templateUrl: './user.component.html',
  styleUrls: ['./user.component.css']
})
export class UserComponent implements OnInit {
  OnInit(){}
  constructor(private router: Router) { }
  
  goDetail() {
    // 页面跳转
    this.router.navigate(['/detail'])
    // 携带参数
    // this.router.navigate(['/detail',12])
    // this.router.navigate(['/detail'],{queryParams:{id:12}})
  }

}

在组件中解析获取动态路径参数

typescript 复制代码
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Location } from '@angular/common';

@Component({
  selector: 'app-user',
  templateUrl: './user.component.html',
  styleUrls: ['./user.component.css']
})
export class UserComponent implements OnInit {

  constructor(
    private route: ActivatedRoute,
    private location: Location
  ) { }

  ngOnInit() {
    const id = this.route.snapshot.paramMap.get('id')
    // 获取queryParams参数
    // const id = this.route.snapshot.queryParams.id
    console.log(id)
  }

}

动态路由匹配

动态路径配置

ts 复制代码
{ path: 'detail/:id', component: HeroDetailComponent }

导航链接

html 复制代码
<a *ngFor="let hero of heroes" class="col-1-4"
    routerLink="/detail/{{hero.id}}">

路由后退

typescript 复制代码
import { Location } from "@angular/common";
@Component(...)
export class UserComponent implements OnInit {
  constructor(private location: Location) {}
  ngOnInit() {}
  onBack(){
		this.location.back();
	}
}
相关推荐
前端 贾公子1 分钟前
从入门到实践:前端 Monorepo 工程化实战(4)
前端
菩提小狗4 分钟前
Sqlmap双击运行脚本,双击直接打开。
前端·笔记·安全·web安全
前端工作日常15 分钟前
我学习到的AG-UI的概念
前端
韩师傅20 分钟前
前端开发消亡史:AI也无法掩盖没有设计创造力的真相
前端·人工智能·后端
XiaoYu200234 分钟前
第12章 支付宝SDK
前端
双向331 小时前
RAG的下一站:检索增强生成如何重塑企业知识中枢?
前端
拖拉斯旋风1 小时前
从零开始:使用 Ollama 在本地部署开源大模型并集成到 React 应用
前端·javascript·ollama
asing1 小时前
🤯 为什么我的收银台在鸿蒙系统“第一次返回”死活拦不住?一次差点背锅的排查实录
前端·harmonyos
德育处主任1 小时前
『NAS』在群晖部署图片压缩工具-Squoosh
前端·javascript·docker
Hao_Harrision1 小时前
50天50个小项目 (React19 + Tailwindcss V4) ✨| ThreeDBackgroundBoxes(3D背景盒子组件)
前端·3d·typescript·react·tailwindcss·vite7