路由处理
#
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 {}
设置路由出口:
<h1>{{title}}</h1> <router-outlet></router-outlet>
设置导航链接:
<ul> <li> <a routerLink="/foo">Go Foo</a> </li> <li> <a routerLink="/bar">Go Foo</a> </li> </ul>
#
#
-
path
-
不能以
/
开头
-
-
component
{ path: '', redirectTo: '/dashboard', pathMatch: 'full' },
#
{ path: 'detail/:id', component: HeroDetailComponent },
导航链接:
<a *ngFor="let hero of heroes" class="col-1-4" routerLink="/detail/{{hero.id}}">
在组件中解析获取动态路径参数:
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') console.log(id) } }
#
this.location.back();
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
2020-03-19 数据库读写分离之配置Django实现数据库读写分离