查看: 55|回复: 0

angular中的动态路由

[复制链接]

0

主题

0

回帖

0

积分

积极分子

金币
0
阅读权限
220
精华
0
威望
0
贡献
0
在线时间
0 小时
注册时间
2009-10-22
发表于 2019-5-23 15:31:00 | 显示全部楼层 |阅读模式

1.配置动态路由

 

const routes: Routes = [
  {path: 'home', component: HomeComponent},
  {path: 'news', component: NewsComponent},
  {path: 'newscontent/:id', component: NewscontentComponent},
  {
    path: '',
    redirectTo: '/home',
    pathMatch: 'full'
} ];

 

2.跳转传值

<a [routerLink]="[ '/newscontent/',aid]">跳转到详情</a> 
<a routerLink="/newscontent/{{aid}}">跳转到详情</a>

3.获取动态路由的值

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 跳转

1. 引入

 

import { Router } from '@angular/router';

 

2.初始化

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

3.路由跳转

 

this.router.navigate(['/news', hero.id]);

 

 


路由 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);
  }

 

3.获取 get 传值

 

   constructor(private route: ActivatedRoute) {
     console.log(this.route.queryParams);
}

 

 

 

 

 

 

 

 

 

 

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

相关侵权、举报、投诉及建议等,请发 E-mail:qiongdian@foxmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部