angular 路由

命令创建项目
  1. 命令创建项目
ng new angualrdemo08 --skip-install
图片.png
  1. 创建需要的组件
ng g component home 
ng g component news 
ng g component newscontent
  1. 找到 app-routing.module.ts 配置路由
  • 引入组件
import { HomeComponent } from './home/home.component';
import { NewsComponent } from './news/news.component'; 
import { NewscontentComponent } from'./newscontent/newscontent.component';
  • 配置路由
const routes: Routes = [ 
{path: 'home', component: HomeComponent}, 
{path: 'news', component: NewsComponent}, 
{path: 'newscontent/:id', component: NewscontentComponent}, 
{ path: '', redirectTo: '/home', pathMatch: 'full' }
 ];
  1. 找到 app.component.html 根组件模板,配置 router-outlet 显示动态加载的路由
<h1><a routerLink="/home">首页</a> <a routerLink="/news">新闻</a> </h1>
 <router-outlet></router-outlet>

Angular routerLink 跳转页面 默认路由

//匹配不到路由的时候加载的组件 或者跳转的路由 
{ path: '**', /*任意的路由*/ // 
component:HomeComponent,
redirectTo:'home' 
}

设置选中样式

<h1>
<a [routerLink]="[ '/home' ]" routerLinkActive="active">首页</a> 
<a [routerLink]="[ '/news' ]" routerLinkActive="active">新闻</a> 
</h1>
.active{ color:red; }

动态路由

配置动态路由

const routes: Routes = [ 
{path: 'home', component: HomeComponent}, 
{path: 'news', component: NewsComponent}, 
{path: 'newscontent/:id', component: NewscontentComponent}, 
{ path: '', redirectTo: '/home', pathMatch: 'full' } 
];
//跳转传值
<a [routerLink]="[ '/newscontent/',aid]">跳转到详情</a> 
<a routerLink="/newscontent/{{aid}}">跳转到详情</a>

获取动态路由的值

import { ActivatedRoute} from '@angular/router';
constructor( private route: ActivatedRoute) { }
ngOnInit() { 
console.log(this.route.params); 
this.route.params.subscribe(data=>this.id=data.id); 
}

动态路由的 js 跳转

import { Router } from '@angular/router';
export class HomeComponent implements OnInit { 
constructor(private router: Router) { } 
ngOnInit() { }
goNews(){ 
// this.router.navigate(['/news', hero.id]); 
this.router.navigate(['/news']); 
} }

路由 get 传值 js 跳转

  1. 引入 NavigationExtras
import { Router ,NavigationExtras} from '@angular/router';

2.定义一个 goNewsContent 方法执行跳转,用 NavigationExtras 配置传参。

goNewsContent(){ 
let navigationExtras: NavigationExtras = { 
queryParams: { 'session_id': '123' }, 
fragment: 'anchor' 
};
this.router.navigate(['/news'],navigationExtras); }
//获取 get 传值
constructor(private route: ActivatedRoute) { console.log(this.route.queryParams); 
}

父子路由

{ 
path: 'news', 
component:NewsComponent, 
children: [ 
{ path:'newslist', component:NewslistComponent },
{ path:'newsadd', component:NewsaddComponent }
 ] 
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 摘要:在本教程中,Ahmed Bouchefra 介绍了angular路由器(router),以及如何使用它创建客...
    哈维尔23456阅读 3,336评论 0 3
  • 一:路由基础 什么是路由: 在web开发中,路由的概念由来已久,简而言之,就是利用URL的唯一性来指定特定的事物,...
    真的稻城阅读 6,038评论 2 7
  • 倾家荡产买入文乃股!!!文乃天外飞仙,唯我文乃(破嗓)!! 任你们前十二集蹦来蹦去,我只打我的助攻,在最佳的时机,...
    thisDong阅读 10,496评论 0 4
  • 第二节:路由介绍 1.生成新项目 ng new 项目名 --routing : 注意 --routing的作...
    咖啡浮点阅读 1,076评论 0 2
  • 官网链接: angular官网 路由与导航最好是跟着官网写一遍代码,然后来看这个总结,会比较清晰 如何实现一个简单...
    H_DaYan阅读 3,264评论 0 6