手动创建TypeScript+Angular2项目的方式运行HelloWorld

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sunxiaoju/article/details/84858634

1、首先创建一个项目文件夹为:angular2-webapp,如下图所示:

2、然后在该文件下创建一个package.json文件,此文件是npm管理包的文件,可以通过此文件的配置安装相应的包,如下代码所示:

{
  "name": "angular2-webapp",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~7.1.0",
    "@angular/common": "~7.1.0",
    "@angular/compiler": "~7.1.0",
    "@angular/core": "~7.1.0",
    "@angular/forms": "~7.1.0",
    "@angular/platform-browser": "~7.1.0",
    "@angular/platform-browser-dynamic": "~7.1.0",
    "@angular/router": "~7.1.0",
    "core-js": "^2.5.4",
    "rxjs": "~6.3.3",
    "tslib": "^1.9.0",
    "zone.js": "~0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.11.0",
    "@angular/cli": "~7.1.0",
    "@angular/compiler-cli": "~7.1.0",
    "@angular/language-service": "~7.1.0",
    "@types/node": "~8.9.4",
    "@types/jasmine": "~2.8.8",
    "@types/jasminewd2": "~2.0.3",
    "codelyzer": "~4.5.0",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~3.1.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~1.1.2",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.11.0",
    "typescript": "~3.1.6"
  }
}

3、创建好文件之后该目录只有一个文件,如下图所示:

4、打开dos窗口进入到该目录中,然后执行npm install即可安装依赖包,如下图所示:

5、下载完成后,如果没出现错误说明安装成功,如下图所示:

6、下载完后的目录如下图所示:

7、新建一个angular.json文件,此文件是配置angular的目录结构对应的文件,包括index.html,main.ts等,文件内容如下:

{
	"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
	"version": 1,
	"newProjectRoot": "projects",
	"projects": {
		"angular2-webapp": {
			"root": "",
			"sourceRoot": "src",
			"projectType": "application",
			"prefix": "app",
			"schematics": {},
			"architect": {
				"build": {
					"builder": "@angular-devkit/build-angular:browser",
					"options": {
						"index": "src/index.html",
						"main": "src/main.ts",
						"tsConfig": "src/tsconfig.app.json",
					}
				},
				"serve": {
					"builder": "@angular-devkit/build-angular:dev-server",
					"options": {
						"browserTarget": "angular2-webapp:build"
					},
					"configurations": {
						"production": {
							"browserTarget": "angular2-webapp:build:production"
						}
					}
				}
			}
		}
	},
	"defaultProject": "angular2-webapp"
}

8、在该配置文件需要一个src目录,在src目录有3个文件,分别是index.html(主页)、main.ts程序入口文件、tsConfig.app.json(typescript编译所需的配置文件),如下图所示:

9、创建一个src文件,如下图所示:

10、然后分别创建三个文件:index.html、main.ts、tsConfig.app.json,如下图所示:

11、main.ts文件内容为:

12、index.html内容如下图所示:

13、tsConfig.app.json文件如下所示:

14、然后在angular2-webapp执行ng serve,会提示错误,如下图所示:

15、出现错误的原因是在angular.json文件中缺少outputPath,只需要添加即可,如下图所示:

16、然后执行ng serve --open,即可开始编译ts文件,如下图所示:

17、在运行时出现错误说是找不到AppModule,这是由于在main.ts文件中不存在AppModule,如下图所示:

18、然后将AppModule更换成AppComponent,如下图所示:

19、然后重新编译运行,在浏览器中会出现NgModule错误,这是由于AppComponent必须在NgModule中,也就是说AppComponent以模块的方式存在,如下图所示:

20、在src中创建一个app目录,并在该目录中创建一个app.component.ts,如下图所示:

21、app.component.ts文件内容如下:

import { Component } from '@angular/core';

@Component({//使用@Component装饰器来定义一个AppComponent组件,Component组件是创建用户界面的主要组件
  selector: 'app',//组件标签,定义之后就可以在html文件中使用<app></app>直接使用该组件了
  template: '<h1>Hello {{title}}',
})
export class AppComponent {
  title = 'World';//标题
}

22、然后在main.ts文件中更改为:

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { NgModule } from '@angular/core';//导入NgModule,NgModule是我们组织Angular应用所必须的
import { BrowserModule } from '@angular/platform-browser';//导入BrowserModule,导入 BrowserModule 是因为它提供了启动和运行浏览器应用的那些基本的服务提供商.
import { AppComponent } from 'app/app.component';

@NgModule({//我们在 @NgModule 的元数据中配置我们导入的模块,因为我们需要依赖 BrowserModule 所以我们在 imports 中添加了它,然后我们又在 declarations 和 bootstrap 选项中添加了 AppComponent 组件.
  declarations: [
    AppComponent//在 declarations添加了 AppComponent 组件
  ],
  imports: [
    BrowserModule
  ],
  providers: [],
  bootstrap: [AppComponent]//bootstrap选项中添加了 AppComponent 组件
})
export class AppModule { }
platformBrowserDynamic().bootstrapModule(AppModule).catch(err => console.error(err));

23、然后重新编译,在浏览器中出现没有Zone.js文件,如下图所示:

24、出现此问题可以通过在angular.json文件中添加polyfills项,并指向一个polyfills.ts,如下图所示:

25、polyfills.ts文件主要是在导入一些包,如下代码所示:

/**
 * This file includes polyfills needed by Angular and is loaded before the app.
 * You can add your own extra polyfills to this file.
 *
 * This file is divided into 2 sections:
 *   1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers.
 *   2. Application imports. Files imported after ZoneJS that should be loaded before your main
 *      file.
 *
 * The current setup is for so-called "evergreen" browsers; the last versions of browsers that
 * automatically update themselves. This includes Safari >= 10, Chrome >= 55 (including Opera),
 * Edge >= 13 on the desktop, and iOS 10 and Chrome on mobile.
 *
 * Learn more in https://angular.io/guide/browser-support
 */

/***************************************************************************************************
 * BROWSER POLYFILLS
 */

/** IE9, IE10 and IE11 requires all of the following polyfills. **/
// import 'core-js/es6/symbol';
// import 'core-js/es6/object';
// import 'core-js/es6/function';
// import 'core-js/es6/parse-int';
// import 'core-js/es6/parse-float';
// import 'core-js/es6/number';
// import 'core-js/es6/math';
// import 'core-js/es6/string';
// import 'core-js/es6/date';
// import 'core-js/es6/array';
// import 'core-js/es6/regexp';
// import 'core-js/es6/map';
// import 'core-js/es6/weak-map';
// import 'core-js/es6/set';

/**
 * If the application will be indexed by Google Search, the following is required.
 * Googlebot uses a renderer based on Chrome 41.
 * https://developers.google.com/search/docs/guides/rendering
 **/
// import 'core-js/es6/array';

/** IE10 and IE11 requires the following for NgClass support on SVG elements */
// import 'classlist.js';  // Run `npm install --save classlist.js`.

/** IE10 and IE11 requires the following for the Reflect API. */
// import 'core-js/es6/reflect';

/**
 * Web Animations `@angular/platform-browser/animations`
 * Only required if AnimationBuilder is used within the application and using IE/Edge or Safari.
 * Standard animation support in Angular DOES NOT require any polyfills (as of Angular 6.0).
 **/
// import 'web-animations-js';  // Run `npm install --save web-animations-js`.

/**
 * By default, zone.js will patch all possible macroTask and DomEvents
 * user can disable parts of macroTask/DomEvents patch by setting following flags
 */

 // (window as any).__Zone_disable_requestAnimationFrame = true; // disable patch requestAnimationFrame
 // (window as any).__Zone_disable_on_property = true; // disable patch onProperty such as onclick
 // (window as any).__zone_symbol__BLACK_LISTED_EVENTS = ['scroll', 'mousemove']; // disable patch specified eventNames

 /*
 * in IE/Edge developer tools, the addEventListener will also be wrapped by zone.js
 * with the following flag, it will bypass `zone.js` patch for IE/Edge
 */
// (window as any).__Zone_enable_cross_context_check = true;

/***************************************************************************************************
 * Zone JS is required by default for Angular itself.
 */
import 'zone.js/dist/zone';  // Included with Angular CLI.


/***************************************************************************************************
 * APPLICATION IMPORTS
 */

26、再次重新ng serve --open,如下图所示:

27、此时在浏览器中运行成功,如下图所示:

猜你喜欢

转载自blog.csdn.net/sunxiaoju/article/details/84858634