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();
	}
}
相关推荐
馬致远12 小时前
Vue todoList案例 优化之本地存储
前端·javascript·vue.js
请叫我聪明鸭12 小时前
CSS实现单行、多行文本超长显示 / 不超长隐藏、悬浮窗超长展示/不超长隐藏、悬浮窗手动控制样式
前端·javascript·css
blackorbird12 小时前
苹果修复了两个在定向攻击中被利用的Webkit漏洞,其中一个与谷歌ANGLE漏洞同源
前端·webkit
席之郎小果冻12 小时前
【04】【创建型】【聊一聊,建造者模式】
java·前端·建造者模式
风无雨12 小时前
在 React 中实现数学公式显示:使用 KaTeX 和 react-katex
前端·react.js·前端框架
zfj32112 小时前
vscode是js开发的,为什么能支持golang java等各种语言开发
javascript·vscode·golang
GDAL13 小时前
Mapbox GL JS 核心表达式:`==` 相等判断完全教程
javascript·mapbox
二两锅巴13 小时前
📺 无需Electron!前端实现多显示器浏览器窗口精准控制与通信
前端
炸土豆13 小时前
防抖节流里的this传递
前端·javascript
用户40993225021213 小时前
Vue3中动态样式数组的后项覆盖规则如何与计算属性结合实现复杂状态样式管理?
前端·ai编程·trae