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();
	}
}
相关推荐
CappuccinoRose9 小时前
JavaScript 学习文档(二)
前端·javascript·学习·数据类型·运算符·箭头函数·变量声明
这儿有一堆花9 小时前
Vue 是什么:一套为「真实业务」而生的前端框架
前端·vue.js·前端框架
全栈前端老曹9 小时前
【MongoDB】深入研究副本集与高可用性——Replica Set 架构、故障转移、读写分离
前端·javascript·数据库·mongodb·架构·nosql·副本集
NCDS程序员9 小时前
v-model: /v-model/ :(v-bind)三者核心区别
前端·javascript·vue.js
夏幻灵10 小时前
CSS三大特性:层叠、继承与优先级解析
前端·css
小杨同学呀呀呀呀10 小时前
Ant Design Vue <a-timeline>时间轴组件失效解决方案
前端·javascript·vue.js·typescript·anti-design-vue
qq_5324535310 小时前
使用 Three.js 构建沉浸式全景图AR
开发语言·javascript·ar
华玥作者18 小时前
[特殊字符] VitePress 对接 Algolia AI 问答(DocSearch + AI Search)完整实战(下)
前端·人工智能·ai
Mr Xu_18 小时前
告别冗长 switch-case:Vue 项目中基于映射表的优雅路由数据匹配方案
前端·javascript·vue.js
前端摸鱼匠19 小时前
Vue 3 的toRefs保持响应性:讲解toRefs在解构响应式对象时的作用
前端·javascript·vue.js·前端框架·ecmascript