@NgModule是新的?

时间:2016-08-16 20:03:47

标签: angular

我正在通过我刚买的书来学习棱角2(成为角2的忍者)。

我也正在同时浏览官方文档,但是当从官方网站快速启动时,我发现他们正在谈论@NgModule,而书中没有提到它。

所以我想知道官方文档是否是最新的,如果NgModule确实是新事物。我想我在某个地方看到该文档不是最新的,所以我不想使用即将被弃用的东西。

以下是他们如何在书中引导应用程序:

import { bootstrap } from '@angular/platform-browser-dynamic';
import PonyRacerAppComponent  from './app/ponyracer-app.component';

bootstrap(PonyRacerAppComponent).catch(err => console.log(err)); 

以下是他们在官方快速入门中引导应用程序的方式:

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import
       { AppComponent }  from './app.component';

@NgModule({
  imports: [ BrowserModule ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

然后在另一个文件中:

// The browser platform with a compiler
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

// The app module
import { AppModule } from './app.module';

// Compile and launch the module
platformBrowserDynamic().bootstrapModule(AppModule);

3 个答案:

答案 0 :(得分:4)

答案 1 :(得分:2)

@NgModule是新的吗? =>是

在angular2 rc5中是的,但我认为它与angular 1.x的概念几乎相同

模块是组织应用程序并使用外部库的功能扩展它的好方法。

许多Angular库都是模块(例如,FormsModule,HttpModule,RouterModule)。许多第三方库都可用作Angular模块(例如,Material Design,Ionic,AngularFire2)。

Angular模块将组件,指令和管道整合到一致的功能块中,每个模块都专注于功能区域,应用程序业务域,工作流程或公用事业集合。

模块还可以向应用程序添加服务。这些服务可能是内部开发的,例如应用程序记录器。它们可以来自外部来源,例如Angular路由器和Http客户端。

应用程序启动时,可以急切地加载模块。它们也可以由路由器异步加载延迟。

Source

答案 2 :(得分:0)

是的,这是Angular 2最终稳定版本中的一个全新集成。

Jecelyn Yeen在https://scotch.io/bar-talk/getting-to-know-angular-2s-module-ngmodule#module-in-angular-2

的文章中对此进行了很好的解释
  

Angular 2中的模块

     

快进到Angular 2. Angular Module现在称为@NgModule。

     

问题1为什么我们需要@NgModule?