forRoot中的重定向

pathMatchfull

  1. const routes: Routes = [{
  2. path: '',
  3. redirectTo: 'sass',
  4. pathMatch: 'full',
  5. }, {
  6. path: 'sass',
  7. children: [{
  8. path: '',
  9. redirectTo: 'cage/spec/group',
  10. pathMatch: 'full',
  11. }, {
  12. path: 'cage/spec/group',
  13. component: CageSpecGroupComponent,
  14. loadChildren: '../../src/cage-spec-group/cage-spec-group.module#CageSpecGroupModule',
  15. }, {
  16. path: 'chain/cage',
  17. component: ChainCageComponent,
  18. loadChildren: '../../src/chain-cage/chain-cage.module#ChainCageModule',
  19. }]
  20. }];

forChild中的重定向

不设置pathMatch

  1. const routes: Routes = [{
  2. path: '',
  3. component: ChainCageComponent,
  4. children: [{
  5. path: '',
  6. redirectTo: 'list',
  7. }, {
  8. path: 'list',
  9. component: ListComponent,
  10. }, {
  11. path: 'create',
  12. component: CreateComponent,
  13. }, {
  14. path: 'update/:id',
  15. component: CreateComponent,
  16. }]
  17. }];

issue

router recursive error

将src目录和example目录加入到tsconfig.json的include中

  1. {
  2. "extends": "../tsconfig.json",
  3. "compilerOptions": {
  4. "outDir": "../out-tsc/app",
  5. "baseUrl": "./",
  6. "module": "es2015",
  7. "types": []
  8. },
  9. "include": [
  10. "../src",
  11. "./"
  12. ],
  13. "exclude": [
  14. "test.ts",
  15. "**/*.spec.ts"
  16. ]
  17. }

主项目

在根项目中使用RouterModule.forRoot()

  1. const routes: Routes = [{
  2. path: '',
  3. redirectTo: 'cage/spec/group',
  4. pathMatch: 'full',
  5. }, {
  6. path: 'cage/spec/group',
  7. component: AppComponent,
  8. loadChildren: '../../src/cage-spec-group/cage-spec-group.module#CageSpecGroupModule',
  9. }];
  10. @NgModule({
  11. imports: [
  12. RouterModule.forRoot(routes)
  13. ],
  14. exports: [RouterModule]
  15. })
  16. export class AppRoutingModule { }

组件项目中的路由控制

在组件项目中使用RouterModule.forChild(),此时所有的相对路由都会加上cage/spec/group前缀

  1. const routes: Routes = [{
  2. path: '',
  3. component: CageSpecGroupComponent,
  4. children: [{
  5. path: 'list',
  6. component: ListComponent,
  7. }, {
  8. path: 'create',
  9. component: CreateComponent,
  10. }]
  11. }];
  12. @NgModule({
  13. imports: [
  14. RouterModule.forChild(routes)
  15. ],
  16. exports: [RouterModule]
  17. })
  18. export class CageSpecGroupRoutingModule { }