Angular之RouterModule的forRoot与forChild

Angular 提供了一种方式来把服务提供商从模块中分离出来,以便模块既可以带着 providers 被根模块导入,也可以不带 providers 被子模块导入。

区别:

`forRoot` creates a module that contains all the directives, the given routes, and the router service itself

`forChild` creates a module that contains all the directives and the given routes, but does not include the router service.

RouterModule的元数据

@NgModule({
    declarations: ROUTER_DIRECTIVES, 
    exports: ROUTER_DIRECTIVES
})

RouterModule定义的指令

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

 

 

原文地址:https://www.cnblogs.com/sea-breeze/p/9106434.html