简体   繁体   English

Angular 2 不同模块相同的路由

[英]Angular 2 Different modules same routing

I am having two main routes, both loading same child module.我有两条主要路线,都加载相同的子模块。 Is there any possible way to have two routes with same name on the child module that loads two different components with respect to the main route.是否有任何可能的方法在子模块上有两条同名的路由,以加载相对于主路由的两个不同组件。

Main Routes :主要路线:

export const ROUTES: Routes = [{
    path: 'first',
    loadChildren: './features/common#CommonModule',
    canActivate: [AppAuthGuard]
}, {
    path: 'second',
    loadChildren: './features/common#CommonModule',
    canActivate: [AppAuthGuard]
}]

Now I'm expecting the common module to have routes something like this现在我期待通用模块有这样的路由

export const routes = [{
        path: 'list', component: FirstListComponent, pathMatch: 'full' }
    },{
        path: 'list', component: SecondListComponent, pathMatch: 'full' }
    }]

So, I want something like所以,我想要类似的东西

  • If route is first/list , then FirstListComponent should be loaded.如果路由是first/list ,则应该加载FirstListComponent
  • If route is second/list , then SecondListComponent should be loaded.如果路由是second/list ,则应加载SecondListComponent

I know that the order of the routes matters.我知道路线的顺序很重要。 And the proposed way is not possible.并且提议的方式是不可能的。 Can anyone suggest any possible way to achieve this.任何人都可以提出任何可能的方法来实现这一目标。

Please set path like this请像这样设置路径

export const routes = [{
        path: 'first/list', component: FirstListComponent, pathMatch: 'full' }
    },{
        path: 'second/list', component: SecondListComponent, pathMatch: 'full' }
    }]

This works for me, and is more simple:这对我有用,而且更简单:

App routing应用路由

const routes: Routes = [
  {
    path: 'mobile',
    loadChildren: './view/mobile/mobile.module#MobileModule',
    canActivate: [MobileGuardService],
    data: {
      preload: false
    }
  },
  {
    path: 'desktop',
    loadChildren: './view/desktop/desktop.module#DesktopModule',
    canActivate: [DesktopGuardService],
    data: {
      preload: false
    }
  },
  {
    path: 'error',
    loadChildren: './view/error/error.module#ErrorModule',
    data: {
      preload: false
    }
  },
  {
    path: '',
    redirectTo: window.innerWidth < 768 ? `/mobile/etc/etc/etc` : `/desktop/etc/etc/etc`,
    pathMatch: 'full'
  },
  {
    path: '**',
    redirectTo: window.innerWidth < 768 ? `/mobile/etc/etc/etc` : `/desktop/etc/etc/etc`
  }
];

Mobile Guard移动卫士

@Injectable({
  providedIn: 'root'
})
export class MobileGuardService implements CanActivate {

  constructor(
    private router: Router
  ) {
  }

  canActivate() {
    if (window.innerWidth >= 768) {
      this.router.navigate(['/']).then();
      return false;
    }
    return true;
  }

}

Desktop Guard桌面卫士

@Injectable({
  providedIn: 'root'
})
export class DesktopGuardService implements CanActivate {

  constructor(
    private router: Router
  ) {
  }

  canActivate() {
    if (window.innerWidth < 768) {
      this.router.navigate(['/m/']).then();
      return false;
    }
    return true;
  }

}

I do this way, becuase redirectTo make problems with guards :(我这样做,因为redirectTo使警卫出现问题:(

:) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM