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();
	}
}
相关推荐
搬砖的阿wei1 天前
CSS常用选择器总结
前端·css
2601_949833391 天前
flutter_for_openharmony口腔护理app实战+意见反馈实现
android·javascript·flutter
Trae1ounG1 天前
Vue Iframe
前端·javascript·vue.js
阿部多瑞 ABU1 天前
`tredomb`:一个面向「思想临界质量」初始化的 Python 工具
前端·python·ai写作
比特森林探险记1 天前
React API集成与路由
前端·react.js·前端框架
爱上妖精的尾巴1 天前
8-1 WPS JS宏 String.raw等关于字符串的3种引用方式
前端·javascript·vue.js·wps·js宏·jsa
hvang19881 天前
某花顺隐藏了重仓涨幅,通过chrome插件计算基金的重仓涨幅
前端·javascript·chrome
Async Cipher1 天前
TypeScript 的用法
前端·typescript
web打印社区1 天前
vue页面打印:printjs实现与进阶方案推荐
前端·javascript·vue.js·electron·html
We་ct1 天前
LeetCode 30. 串联所有单词的子串:从暴力到高效,滑动窗口优化详解
前端·算法·leetcode·typescript