Angular2 app module with root level imports

后端 未结 2 2074
一个人的身影
一个人的身影 2020-12-16 13:37

Shouldn\'t imports on the root level be available globally (With globally I mean to all sub-modules and components)?

I have the following root/app module :



        
2条回答
  •  盖世英雄少女心
    2020-12-16 13:58

    Angular's documentation is confusing, they state in several areas that modules you import in the root module(app.module.ts) are available globally, but what is not quickly evident unless you read thoroughly (blah) is that when you use a submodule it breaks that inheritance and a submodule creates it's own app domain for components (not services, services are still global, at least the way I read it). That's also why if you were like me, I noticed some items I had to import into my sub-module, and others I didn't which really confused me. Here's the section on angular.io that discusses this: NgModule.html

    Something else worth mentioning: I thought developing in Angular2 since RC5+ meant that I needed to wrap all my features up into modules, this is simply not the case. Modules aren't really necessary for us to build in our general day to day work with Angular2 unless you want to use lazy loading or are specifically desiring to share code with others, for example an npm package.

提交回复
热议问题