Angular 2 高级配置


在本章中,我们将查看 Angular 2 项目中的其他配置文件。

tsconfig.json


该文件用于提供有关用于 Angular JS 项目的 TypeScript 的选项。

{ 
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "moduleResolution": "node",
        "sourceMap": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "lib": [ "es2015", "dom" ],
        "noImplicitAny": true,
        "suppressImplicitAnyIndexErrors": true
    }
}

以下是有关上述代码的一些要点。

  • 编译的目标是 es5,这是因为大多数浏览器只能理解 ES5 typescript。

  • sourceMap 选项用于生成 Map 文件,这在调试时很有用。因此,在开发过程中,最好将此选项保持为真。

  • Angular JS 装饰器需要 "emitDecoratorMetadata": true 和 "experimentalDecorators": true 。如果没有到位,Angular JS 应用程序将无法编译。

包.json


该文件包含有关 Angular 2 项目的信息。以下是文件中的典型设置。

{
    "name": "angular-quickstart",
    "version": "1.0.0",
    "description": "QuickStart package.json from the documentation,
        supplemented with testing support",
   
    "scripts": {
        "build": "tsc -p src/",
        "build:watch": "tsc -p src/ -w",
        "build:e2e": "tsc -p e2e/",
        "serve": "lite-server -c=bs-config.json",
        "serve:e2e": "lite-server -c=bs-config.e2e.json",
        "prestart": "npm run build",
        "start": "concurrently \"npm run build:watch\" \"npm run serve\"",
        "pree2e": "npm run build:e2e",
        "e2e": "concurrently \"npm run serve:e2e\" \"npm run protractor\"
            --killothers --success first",
        "preprotractor": "webdriver-manager update",
        "protractor": "protractor protractor.config.js",
        "pretest": "npm run build",
        "test": "concurrently \"npm run build:watch\" \"karma start karma.conf.js\"",
        "pretest:once": "npm run build",
        "test:once": "karma start karma.conf.js --single-run",
        "lint": "tslint ./src/**/*.ts -t verbose"
    },

    "keywords": [],
    "author": "",
    "license": "MIT",
    "dependencies": {
        "@angular/common": "~2.4.0",
        "@angular/compiler": "~2.4.0",
        "@angular/core": "~2.4.0",
        "@angular/forms": "~2.4.0",
        "@angular/http": "~2.4.0",
        "@angular/platform-browser": "~2.4.0",
        "@angular/platform-browser-dynamic": "~2.4.0",
        "@angular/router": "~3.4.0",
        "angular-in-memory-web-api": "~0.2.4",
        "systemjs": "0.19.40",
        "core-js": "^2.4.1",
        "rxjs": "5.0.1",
        "zone.js": "^0.7.4"
    },

    "devDependencies": {
        "concurrently": "^3.2.0",
        "lite-server": "^2.2.2",
        "typescript": "~2.0.10",
        "canonical-path": "0.0.2",
        "tslint": "^3.15.1",
        "lodash": "^4.16.4",
        "jasmine-core": "~2.4.1",
        "karma": "^1.3.0",
        "karma-chrome-launcher": "^2.0.0",
        "karma-cli": "^1.0.1",
        "karma-jasmine": "^1.0.2",
        "karma-jasmine-html-reporter": "^0.2.2",
        "protractor": "~4.0.14",
        "rimraf": "^2.5.4",
        "@types/node": "^6.0.46",
        "@types/jasmine": "2.5.36"
    },
    "repository": {}
}

上面代码需要注意的一些关键点:

  • 有两种类型的依赖,第一种是依赖,然后是开发依赖。在开发过程中需要开发者,而其他人则需要运行应用程序。

  • "build:watch": "tsc -p src/ -w" 命令用于通过查找打字稿文件中的更改来在后台编译打字稿。

systemjs.config.json


该文件包含 Angular JS 应用程序所需的系统文件。这将加载所有必要的脚本文件,而无需向 html 页面添加脚本标记。典型文件将具有以下代码。

/** 
 * System configuration for Angular samples 
 * Adjust as necessary for your application needs. 
*/ 
(function (global) { 
    System.config ({
        paths: {
            // 路径作为别名
            'npm:': 'node_modules/'
        },
      
        // map 告诉系统加载器在哪里寻找东西
        map: {
            // 我们的应用在 app 文件夹中
            app: 'app',
         
            // 角度束
            '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
            '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
            '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
            '@angular/platform-browser': 'npm:@angular/platformbrowser/bundles/platform-browser.umd.js',
            '@angular/platform-browser-dynamic':
                'npm:@angular/platform-browserdynamic/bundles/platform-browser-dynamic.umd.js',
            '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
            '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
            '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
         
            // 其他库
            'rxjs':  'npm:rxjs',
            'angular-in-memory-web-api':
                'npm:angular-in-memory-web-api/bundles/inmemory-web-api.umd.js'
        },
     
        // packages 告诉系统加载器在没有文件名时如何加载
            and/or no extension
        packages: {
            app: {
                defaultExtension: 'js'
            },
         
            rxjs: {
                defaultExtension: 'js'
            }
        }
   
    });
})(this); 

上面代码需要注意的一些关键点:

  • 'npm:': 'node_modules/' 告诉我们项目中所有 npm 模块所在的位置。

  • app: 'app' 的映射告诉我们所有应用程序文件的加载文件夹。