【问题标题】:What does empty .forRoot() exactly do in angular2? [duplicate]空 .forRoot() 在 angular2 中究竟做了什么? [复制]
【发布时间】:2018-03-23 12:24:43
【问题描述】:

我见过人们在他们的项目中使用 .forRoot() 以及主要的 forRoot(paths inside...) 将他们带到子网址。

root这个空的目的是什么?

示例(用于定义模块的导入):

    NgbModule.forRoot(),
    ShareButtonsModule.forRoot(),
    BrowserModule,
    HttpClientModule,

【问题讨论】:

    标签: javascript angularjs module routing ng-modules


    【解决方案1】:

    forRoot 是模块上静态类方法的约定。

    它用于将模块的某些提供者保持为singletons,这意味着它们只被注入到主应用程序模块中,而不是被注入到单个组件中。

    这样,您可以在主应用模块中使用MyModule.forRoot()only,并且仍然将MyModule 导入到需要它的组件中。

    forRoot(...) 内部传递的数据取决于模块本身和它拥有的提供程序。不管forRoot是空的还是带参数的,关键是只在主app模块中使用

    Here is a good article discussing forRoot() in detail.

    【讨论】:

      猜你喜欢
      • 2019-03-06
      • 1970-01-01
      • 1970-01-01
      • 2019-02-15
      • 2015-09-15
      • 2011-06-18
      • 2012-07-23
      • 2016-09-10
      • 2023-03-15
      相关资源
      最近更新 更多