2017-06-07 24 views
1

在Angular路由的文檔中,它顯示了一個功能模塊以及如何使用靜態功能。爲什麼在Angular文檔示例中導出RouterModule?

在該示例中,它顯示了由功能模塊導入和導出的RouterModule。

import { NgModule }    from '@angular/core'; 
import { RouterModule, Routes } from '@angular/router'; 

import { CrisisListComponent } from './crisis-list.component'; 
// import { HeroListComponent } from './hero-list.component'; // <-- delete this line 
import { PageNotFoundComponent } from './not-found.component'; 

const appRoutes: Routes = [ 
    { path: 'crisis-center', component: CrisisListComponent }, 
    // { path: 'heroes',  component: HeroListComponent }, // <-- delete this line 
    { path: '', redirectTo: '/heroes', pathMatch: 'full' }, 
    { path: '**', component: PageNotFoundComponent } 
]; 

@NgModule({ 
    imports: [ 
     RouterModule.forRoot(appRoutes) 
    ], 
    exports: [ 
     RouterModule 
    ] 
}) 
export class AppRoutingModule {} 

https://angular.io/docs/ts/latest/guide/router.html#!#remove-duplicate-hero-routes

我不明白爲什麼RouterModule出口或看到文檔中的任何原因。

回答

1

導出模塊本質上導出在此模塊上定義的所有聲明(指令,組件,管道)。

路由模塊定義了一組路由器特定指令:

const ROUTER_DIRECTIVES = [RouterOutlet, RouterLink, RouterLinkWithHref, RouterLinkActive]; 

他們註冊到RouterModule:

如果你想使用它們裏面的模塊組件的模板,你有將該模塊導入您的功能模塊。但是,您也可以將RouterModule僅導入到共享模塊AppRoutingModule中,然後將其導入其他位置而不導入RouterModule

+0

太棒了。這會讓生活更輕鬆。謝謝。 – cgTag

+1

不客氣:)我對Angular [中](https://medium.com/@maximus.koretskyi)進行了深入的寫作,您可能想要查看它們 –

相關問題