我是Angular的新手。我已经学习了Angular的基本概念,了解了项目的结构。
但是当我用Angular-CLI生成一个基本项目时,我发现在项目的根目录src中没有NgModule。但是根据Angular主页上的文档,根文件是NgModule。所以我的问题是,它们之间的区别是什么,为什么Angular-CLI使用组件作为根?谢谢。
我想angular-cli还没有更新。Angular 2仍然在快速变化。
但是,你可以很容易地自己创建它:
$ ng new yourproject
然后导航到yourproject/src/
文件夹,替换main.ts
如下:
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
platformBrowserDynamic().bootstrapModule(AppModule);
进入yourproject/src/app
文件夹,创建app.module.ts
:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { MdButtonModule } from '@angular2-material/button';
import { AppComponent } from './app.component';
@NgModule({
imports: [ BrowserModule ],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
您可能还需要在yourproject/package.json
中添加/更新一些依赖项。以下是我的:
"dependencies": {
"@angular/common": "2.0.0-rc.5",
"@angular/compiler": "2.0.0-rc.5",
"@angular/core": "2.0.0-rc.5",
"@angular/forms": "0.3.0",
"@angular/http": "2.0.0-rc.5",
"@angular/platform-browser": "2.0.0-rc.5",
"@angular/platform-browser-dynamic": "2.0.0-rc.5",
"@angular/router": "3.0.0-rc.1",
"@angular/router-deprecated": "2.0.0-rc.2",
"@angular/upgrade": "2.0.0-rc.5",
"bootstrap": "^3.3.6",
"core-js": "^2.4.0",
"es6-shim": "0.35.1",
"reflect-metadata": "^0.1.3",
"rxjs": "5.0.0-beta.6",
"systemjs": "0.19.27",
"zone.js": "^0.6.12"
},
"devDependencies": {
"angular-cli": "1.0.0-beta.10",
"codelyzer": "0.0.20",
"ember-cli-inject-live-reload": "1.4.0",
"jasmine-core": "2.4.1",
"jasmine-spec-reporter": "2.5.0",
"karma": "0.13.22",
"karma-chrome-launcher": "0.2.3",
"karma-jasmine": "0.3.8",
"protractor": "3.3.0",
"ts-node": "0.5.5",
"tslint": "^3.7.4",
"typescript": "^1.8.10",
"typings": "1.3.1"
}
这是因为到目前为止,据我所知,Angular2 cli运行在rc4而不是rc5上,默认的npm安装,你看到的例子是rc5库(NgModule),而不是rc4,你应该选择最方便的版本,因为发布候选版本倾向于快速更新。