如何获取子模块的根路径?

时间:2017-11-02 02:25:20

标签: angular angular2-routing

我有一个具有自己路由的子模块。在app-routing.module(顶部模块)内,只要用户转到网址/show,就会延迟加载:

const routes: Routes = [
  {
    path: 'show',
    loadChildren: './show/show.module#ShowModule'
  }
];

从ShowModule及其组件中,如何找到此子模块的 root ,即/show

我需要它来构建路由器链接。例如,我想在此Show子模块中创建一个名为“product”的首页的链接。完整的网址应为/show/product。我尝试使用<a routerLink="/product">但完整网址只是/product

我计划使用类似<a [routerLink]="[rootPath, 'product']">的内容,但不知道如何获取'rootPath'变量。

2 个答案:

答案 0 :(得分:0)

创建这样的子模块路由,这样当你调用show route automaticaaly时,它会调用showModule''route,如果你想创建子节点你可以创建,但首先调用''(空路径)

const routes: Routes = [
  {
    path: '',
   component:''
  }
];

答案 1 :(得分:0)

ShowModule中的路由器数组应该是:

const routes:Routes = [
    {
     path: '', 
     component: ShowComponent, 
     children: [ 
        {
           path: 'product,
           component: ProductComponent
        }
     ]
    }
];

在ShowComponent的HTML的某些部分中,您需要包含

但是如果你不想拥有一个ShowComponent,你可以这样省略它:

const routes:Routes = [
    {
     path: 'product', 
     component: ProductComponent
    }
];

如果要从ShowModule中访问Component,则routerOutlet必须

routerOutlet="show/product"

另外,如果你想在ShowModule里面访问,routerModule必须是

routerOutlet="product"

希望它可以帮助!!

相关问题