对Angular 2中提供的@NgModule感到困惑 [英] Confused about @NgModule provided in Angular 2

查看:78
本文介绍了对Angular 2中提供的@NgModule感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试玩Angular2,当我将基本代码升级到Angular 2.0.0-RC5时,我发现引入了一个重大更改@NgModule,它等效于 angular.moudle() >在Angularjs 1.x中?

I am trying to play Angular2, and when I upgraded the base codes to Angular 2.0.0-RC5, I found a breaking changes introduced, @NgModule, which is equivalent with angular.moudle() in Angularjs 1.x?

对我来说,@ ngModule使事情变得复杂.

当我使用Angular 1.x时,社区中有人对angular.module有所抱怨,因为存在一些javascript模块系统.

When I was using Angular 1.x, there are some complaints in communities about angular.module, because there are some javascript module systems existed.

默认情况下,Angular-cli生成的代码基于SystemJS,我们可以使用Typescirpt来组织源代码的结构.

By default, Angular-cli generated codes are based SystemJS, we can use Typescirpt to organize the structure of source codes.

为什么我们需要@NgModule,它是应用程序组织在运行时的逻辑视图?

Why we need @NgModule, it is a logic view of the application organization at runtime?

我假设为Angular2的基于模块的体系结构指定了@NgModule,并且可以在不同的应用程序中重用@ngModule.与我过去几年使用的其他基于模块/捆绑/插件的框架相比.它缺少一些功能.

I assume a @NgModule is designated for module based architecture for Angular2, and a @ngModule can be reused in different applications. Compare to other module/bundle/plugin based frameworks I have used in the past years. It lacks some features.

  1. @NgModule提供导入/导出功能,但是如何导入没有Typescript import 子句的模块,以及如何拒绝其他@NgModule导入@NgModule私有组件/管道/服务.
  2. 没有@NgModule的路由前缀,想象一下@NgModule由3party团队维护.例如.用于任何基于帖子的应用程序的通用 Comment 模块,如何避免路由冲突,如何覆盖路由定义,如何在主机@NgModule中配置@ngModule?
  3. 为什么路由配置是由RouteModule.forRoot()(或forChild)注册的,而不是@NgModule批注的 route 属性.

  1. @NgModule provides import/export features, but how to import modules without Typescript import clause, and refuse other @NgModule to import @NgModule private components/pipes/services.
  2. There is no route prefix of a @NgModule, imagine t```he @NgModule is maintained by an 3party team. eg. a common Comment module for any post based application, how to avoid route conflict, how to override the route definitions, how to configure a @ngModule in the host @NgModule?
  3. Why route config is registered by RouteModule.forRoot()(or forChild) not a route attribute of @NgModule annotation like this.

@NgModule(route:{prefix:'/comment', config: commentRouteConfigs}) export class CommentModule{}

@NgModule(route:{prefix:'/comment', config: commentRouteConfigs}) export class CommentModule{}

当前,@ NgModule API耦合了路由配置.

Currently, the @NgModule API coupled route configs.

当我在主机模块中导入模块时,如何覆盖@NgModule中定义的组件模板.

How to override the component template defined in a @NgModule when I import a module in a host module.

谢谢.

推荐答案

据我所知,NgModule主要是为了正确支持路由器对路由和组件的延迟加载而引入的.

As far as I know NgModule was mainly introduced to properly support lazy loading of routes and components by the router.

  1. NgModule的导入和导出与TS导入和导出无关.它主要是关于依赖项注入和编译模板的.

  1. NgModules import and export is not related to TS import and export. It is mostly about dependency injection and compiling templates.

不知道

不明白这个问题

覆盖模板.有一些构建工具可以做到这一点,但是Angular2本身并没有提供任何工具.

Overriding templates is not supported by Angular2 at all. There are build tools that allow that, but Angular2 itself doesn't provide anything to do that.

我已经看到讨论中提到的模块生命周期回调,但是还找不到任何具体的东西(例如

I have seen module lifecycle callbacks seen mentioned in discussions but couldn't find anything concrete yet (like https://github.com/angular/angular/issues/10808#issuecomment-239823985)

我想有可能,但是我不知道Angular2团队是否会考虑这一点.

I guess it's possible but I have no idea if the Angular2 team does consider this.

这篇关于对Angular 2中提供的@NgModule感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆