angularjs2进阶教程5-路由
2017-03-06 22:13
260 查看
还是 angularjs2入门1-文件结构分析 的源码,将app名称tutorial-step5-router
添加列表视图,两种不同的方式来显示列表。点击列表中的某一项,进入英雄的详情。
详细介绍可以参考angularjs2进阶教程-项目介绍
我们要添加 Angular’s Router。
上节课中已经有了appcomponent将列表和详情显示到一个页面上。
创建一个新的dashboard component列表到路由中。
新建hero-detail.component.ts显示英雄详情。
把appcomponent改为应用程序处理导航定位(2种显示方式:dashboard 和原先的列表,1个导航到详情),和显示英雄列表。
locally, only in this file)
Selector
创建app.component.ts,修改app.module.ts
添加Angular Router
RouterModule.forRoot设置初始导航,RouterOutlet显示路由中的内容
Set the
to
module-relative loading of the
在路由中,每个组件都要设置moduleId: module.id,这样为了在导航到某个路由时,加载相应的页面
1.创建dashboard.component.ts
2.新建hero-detail.component.ts
添加列表视图,两种不同的方式来显示列表。点击列表中的某一项,进入英雄的详情。
详细介绍可以参考angularjs2进阶教程-项目介绍
我们要添加 Angular’s Router。
上节课中已经有了appcomponent将列表和详情显示到一个页面上。
创建一个新的dashboard component列表到路由中。
新建hero-detail.component.ts显示英雄详情。
把appcomponent改为应用程序处理导航定位(2种显示方式:dashboard 和原先的列表,1个导航到详情),和显示英雄列表。
app.component.tsfile to
heroes.component.ts
AppComponentclass to
HeroesComponent(rename
locally, only in this file)
Selector
my-appto
my-heroes
创建app.component.ts,修改app.module.ts
添加Angular Router
RouterModule.forRoot设置初始导航,RouterOutlet显示路由中的内容
Set the
moduleIdproperty
to
module.idfor
module-relative loading of the
templateUrl.
在路由中,每个组件都要设置moduleId: module.id,这样为了在导航到某个路由时,加载相应的页面
1.创建dashboard.component.ts
@Component({ moduleId: module.id, selector: 'my-dashboard', templateUrl: './dashboard.component.html', styleUrls: [ './dashboard.component.css' ] }) export class DashboardComponent implements OnInit { heroes: Hero[] = []; constructor(private heroService: HeroService) { } ngOnInit(): void { this.heroService.getHeroes() .then(heroes => this.heroes = heroes.slice(1, 5)); } }
2.新建hero-detail.component.ts
@Component({ moduleId: module.id, selector: 'my-hero-detail', templateUrl: './hero-detail.component.html', styleUrls: [ './hero-detail.component.css' ] }) export class HeroDetailComponent implements OnInit { hero: Hero; constructor( private heroService: HeroService, private route: ActivatedRoute, private location: Location ) {} ngOnInit(): void { this.route.params .switchMap((params: Params) => this.heroService.getHero(+params['id'])) .subscribe(hero => this.hero = hero); } goBack(): void { this.location.back(); } }3.建立路由组件app-routing.module.ts
const routes: Routes = [ { path: '', redirectTo: '/dashboard', pathMatch: 'full' }, { path: 'dashboard', component: DashboardComponent }, { path: 'detail/:id', component: HeroDetailComponent }, { path: 'heroes', component: HeroesComponent } ]; @NgModule({ imports: [ RouterModule.forRoot(routes) ], exports: [ RouterModule ] })4.app.component.ts
@Component({ moduleId: module.id, selector: 'my-app', template: ` <h1>{{title}}</h1> <nav> <a routerLink="/dashboard" routerLinkActive="active">Dashboard</a> <a routerLink="/heroes" routerLinkActive="active">Heroes</a> </nav> <router-outlet></router-outlet> `, styleUrls: ['./app.component.css'], })
相关文章推荐
- vue 基础-->进阶 教程(3):组件嵌套、组件之间的通信、路由机制、slot传值
- angularjs2进阶教程2-建立一个主/详细页面的英雄名单
- Android高手进阶教程 (一)-------Android常用名令集锦
- Android高手进阶教程(七)之 ----Android 中Preferences的使用!
- Android高手进阶教程(二十)之---Android与JavaScript方法相互调用!
- Hadoop基础教程-第7章 MapReduce进阶(7.7 MapReduce 全排序)
- Android高手进阶教程(五)之----Android 中LayoutInflater的使用!
- ASP.NET MVC3 系列教程 - URL友好化的重型武器[路由]
- ReportStudio进阶教程(二十九) - 地图开发(三)追溯报表
- [PHP]进阶教程:双引号和单引号在PHP中的区别以及字符串的基本操作
- Android高手进阶教程(一)-------Android常用名令集锦(图文并茂)!
- Android高手进阶教程(七)之----Android 中Preferences的使用!
- Angular2入门系列教程6-路由(二)-使用多层级路由并在在路由中传递复杂参数
- EXTJS初级UI设计进阶教程(2)
- TensorFlow 教程 --进阶指南--3.2变量:创建、初始化、保存和加载
- ReportStudio进阶教程(三十九) - 转发器
- python进阶教程之函数参数的多种传递方法
- Android高手进阶教程(五)之----Android 中LayoutInflater的使用!
- Android高手进阶教程(十一)之----Android 通用获取Ip的方法(判断手机是否联网的方法)!!!
- RouterOS软路由器、双线策略路由、***借线策略路由、软路由教程、RouterOS v3.0(一)