当前位置: 首页 > news >正文

Angular【router路由】

基本用法

生成路由模块文件

ng generate module app-routing --flat --module=app
// 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不能以 / 开头

设置路由出口

<h1>{{title}}</h1>
<router-outlet></router-outlet>

重定向

{ path: '', redirectTo: '/dashboard', pathMatch: 'full' },

路由跳转

标签跳转

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

<ul><li><a routerLink="/foo">Go Foo</a></li><li><a routerLink="/bar">Go Foo</a></li>
</ul>

编程跳转

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}})}}

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

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.idconsole.log(id)}}

动态路由匹配

动态路径配置

{ path: 'detail/:id', component: HeroDetailComponent }

导航链接

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

路由后退

import { Location } from "@angular/common";
@Component(...)
export class UserComponent implements OnInit {constructor(private location: Location) {}ngOnInit() {}onBack(){this.location.back();}
}
http://www.dtcms.com/a/549723.html

相关文章:

  • 如何网站建设网页宣传片拍摄报价明细
  • 曲阜做网站智能建造概论
  • 嘉峪关网站seo英文商城网站
  • 易混淆的点:栈的增长方向 和 缓冲区内的数据写入方向是相反的
  • 全流程掌握生态环评核心技术——涵盖生物量模拟、生物多样性计算、脆弱度评价及公路铁路、机场、水利项目实战
  • 【Embedded System】嵌入式C语言基础知识
  • PsTools 学习笔记(7.4):PsExec —— 远程进程的退出与控制台输出重定向
  • 双端迭代器:从 `next_back()` 到零拷贝“滑动窗口”——Rust DoubleEndedIterator 全景指南
  • 模型过拟合基本解决办法说明(个人学习向)
  • 自己架设服务器做网站厦门网络推广外包
  • 八年级信息做网站所用软件网站备案核实单
  • 如何用 Python xlwings库自动化操作 Excel?
  • 基于MATLAB的梯度投影稀疏重建算法
  • [特殊字符] FBro工作流自动化平台 - 让浏览器自动化更简单
  • JAVA后端结合网页搜图+阿里万相2.5实现自动化修图与返回
  • 和平区网站制作手机企业网站怎么做
  • 如何在好医生网站做二类学分dede中英文网站切换
  • Kubernetes 实战入门内容
  • 变量声明与可变性
  • 前端生产部署完全指南:从零到精通
  • 做外贸的人常用的网站wordpress视频站模板下载
  • Oracle数据库安装(Windows)
  • 跳转指令介绍
  • 关于网站制作的论文个人网页效果图
  • 评估agent能力benchmark收集汇总
  • 番禺五屏网站建设西安网站建设xs029
  • 深入剖析 Rust `HashMap`:安全哈希 (SipHash) 与高性能冲突处理 (Swiss Table)
  • 一款漏洞库批量下载更新工具,便于在离线情况下漏洞的访问检索
  • 做任务的网站源码活动线报资源网
  • 【Oceanbase】OceanBase批量写入性能优化实战:转储、日志归档、多并发