Angular2学习之懒加载(Lazy)路由(router)问题
来源:互联网 发布:实现即时通讯的php文件 编辑:程序博客网 时间:2024/06/15 05:47
路由是Angular2中经常用到的.而且在Angular2中添加了懒加载(lazy).
问题描述:
上面为文件目录.具体是在hero-routing.module.ts将上面三个文件夹中各组件绑定.
绑定代码:
import {RouterModule} from "@angular/router";import {NgModule} from "@angular/core";import {DashboardComponent} from "./dashboard/dashboard.component";import {HeroesComponent} from "./heroes/heroes.component";import {HeroDetailComponent} from "./hero-detail/hero-detail.component";import {HeroSearchComponent} from "./hero-search/hero-search.component";@NgModule({ imports: [RouterModule.forChild([ { path: '', component: HeroSearchComponent, children: [ { path: 'dashboard', component: DashboardComponent }, { path: 'heroes', component: HeroesComponent }, { path: 'detail/:id', component: HeroDetailComponent }, ] } ])], exports: [RouterModule]})export class HeroRoutingModule {}
点击以上item跳转到detail详情中。
在此问题出现了
找不到路由。
解决方法:
1、修改跳转事件
constructor( private router: Router, private route: ActivatedRoute, private heroService: HeroService) { }
添加ActivateRoute导入
this.router.navigate(['../detail', this.selectedHero.id],{relativeTo: this.route});
其中relativeTo是相对路径,因为hero-routing.module.ts文件没和“跳转路由”在相同文件夹下,所以使用相对路径来寻找。若不添加
{relativeTo: this.route}则在根目录中的路由中寻找
2、界面路径修改
<a class="button" routerLink="{{'../detail/'+selectedHero.id}}" routerLinkActive="active">查看详请</a>
参考:[Angular2 Router] Programmatic Router Navigation via the Router API - Relative And Absolute Router Navigation
[Angular2 Router] Optional Route Query Parameters
0 0
- Angular2学习之懒加载(Lazy)路由(router)问题
- Angular2(五)--router
- vue2路由view-router配置(懒加载)
- angular2之路由与导航(一)
- Nodejs学习(二)--路由(router)
- 学习 ui-router (四): 路由控制
- 学习 ui-router (五): 路由控制
- vue-router学习二(动态路由)
- AngularJS路由之ui-router(一)
- AngularJS路由之ui-router(二)
- AngularJS路由之ui-router(三)
- vue-router实现路由懒加载
- angular2 router中的路由跳转navigate
- angular2 router中的路由跳转navigate
- hibernate懒加载(lazy加载)
- Angular2 父子路由问题
- vue-router路由(一)
- ANGULAR2 深入学习路由
- Log4j日志记录器
- 判断字段在哪些表
- Apache Kylin的快速数据立方体算法——概述
- Integer 的小知识
- Spring beans架构--set注入
- Angular2学习之懒加载(Lazy)路由(router)问题
- Gradle学习笔记(三)java项目编译
- 瀑布流封装
- 安卓学习笔记-3
- 读《聪明人用方格笔记本》有图了有内容了
- mysql-5.5.33编译安装
- 学习python的第二十四天-Web
- POJ 1477 Box of Bricks G++
- 一些题目