Angular 2 升級 Angular 4 歷程

接灰的電子產品發表於2017-03-25

紙書出版了,比網上內容豐富充實了,歡迎大家訂購!
京東連結:item.m.jd.com/product/120…

Angular 2 升級 Angular 4 歷程
Angular從零到一

第一節:初識Angular-CLI
第二節:登入元件的構建
第三節:建立一個待辦事項應用
第四節:進化!模組化你的應用
第五節:多使用者版本的待辦事項應用
第六節:使用第三方樣式庫及模組優化用
第七節:給元件帶來活力
Rx--隱藏在 Angular 中的利劍
Redux 你的 Angular 應用
第八節:查缺補漏大合集(上)
第九節:查缺補漏大合集(下)

Angular 4 在昨天(2017-03-24)正式釋出了,我的系列教程也得更新一下。步驟略繁瑣,不用 cli 的專案反倒更簡單一些,但是 cli 平時給我們的便利還是很多的,升級最多半年一次而已,麻煩就麻煩點吧。

更新 angular-cli

隨著 Angular 升級到版本 4, angular-cli 也進入了 1.0 正式版。所以我們需要先更新 angular-cli 的版本。 首先需要刪除舊的 angular-cli,如果你的 angular-cli 是在 beta-28 之前的版本,需要在工程目錄下執行下面的命令:

npm uninstall -g angular-cli
npm uninstall --save-dev angular-cli複製程式碼

angular-clibeta-28 之後改了包名,併入 @angular 的子專案,包名改成了 @angular/cli,所以如果是 beta-28 之後的版本,請執行下面的命令刪除:

npm uninstall -g @angular/cli
npm uninstall --save-dev @angular/cli複製程式碼

然後我們需要安裝新的 @angular/cli,但進行之前需要清理一下快取:

npm cache clean
npm install -g @angular/cli@latest複製程式碼

更新 angular-cli.json

工程根目錄下的 angular-cli.json 現在的命名前面多了一個點,變成了 .angular-cli.json,雖然舊的命名仍被接受,但為了保險起見,我們還是改一下。

這個 .angular-cli.json 比原來的版本改動了幾個地方,第一個是 Schema,我們需要在 "project": { 之上新增一條 Schema 的配置:

  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "project": {
    ...
  },複製程式碼

新增完之後,在 VSCode 中會發現 project 配置中的 version 下面出現了綠線警告,也就是說 schema 中沒有這一項,所以 version 可以刪除了。

maintest 之間插入一行配置 polyfills:

"main": "main.ts",
"polyfills": "polyfills.ts",
"test": "test.ts",複製程式碼

並且刪除 src/main.tssrc/test.ts 中的 import './polyfills.ts'; 那一行。

然後把下面 "tsconfig": "tsconfig.app.json", 這句改成下面的2行:

"tsconfig": "tsconfig.app.json",
"testTsconfig": "tsconfig.spec.json",複製程式碼

接下來是 environments 的那段,原來的樣子是

"environments": {
  "source": "environments/environment.ts",
  "dev": "environments/environment.ts",
  "prod": "environments/environment.prod.ts"
}複製程式碼

現在需要改成:

"environmentSource": "environments/environment.ts",
"environments": {
  "dev": "environments/environment.ts",
  "prod": "environments/environment.prod.ts"
}複製程式碼

現在新增了 lint 配置,在 e2etest 之間加入:

  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "lint": [
    {
      "project": "src/tsconfig.app.json"
    },
    {
      "project": "src/tsconfig.spec.json"
    },
    {
      "project": "e2e/tsconfig.e2e.json"
    }
  ],
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },複製程式碼

最後一段 defaults 那一坨改成:

"defaults": {
  "styleExt": "css", //或者 scss 根據專案情況而定 
  "component": {
    "inlineTemplate": false,
    "spec": true
  }
}複製程式碼

更新 tsconfig

src/tsconfig.json 需要改名成 tsconfig.app.json 並更新到下面的樣子:

{
  "compilerOptions": {
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "lib": [
      "es2016",
      "dom"
    ],
    "outDir": "../out-tsc/app",
    "module": "es2015",
    "baseUrl": "",
    "types": []
  },
  "exclude": [
    "test.ts",
    "**/*.spec.ts"
  ]
}複製程式碼

新增加定義單元測試配置的 src/tsconfig.spec.json

{
  "compilerOptions": {
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [
      "es2016",
      "dom"
    ],
    "outDir": "../out-tsc/spec",
    "module": "commonjs",
    "target": "es5",
    "baseUrl": "",
    "types": [
      "jasmine",
      "node"
    ]
  },
  "files": [
    "test.ts"
  ],
  "include": [
    "**/*.spec.ts",
    "**/*.d.ts"
  ]
}複製程式碼

e2e 目錄下原有的 tsconfig.json 改名成 e2e/tsconfig.e2e.json 然後更新成:

{
  "compilerOptions": {
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [
      "es2016"
    ],
    "outDir": "../out-tsc/e2e",
    "module": "commonjs",
    "target": "es5",
    "types":[
      "jasmine",
      "node"
    ]
  }
}複製程式碼

在專案根目錄下新建一個 tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "baseUrl": "src",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2016",
      "dom"
    ]
  }
}複製程式碼

更新 package.json

更新 package.json 中的軟體包版本號

dependencies 段落中的:

  1. 所有以 @angular 開頭的包的版本都改成 ^4.0.0
  2. rxjs 版本改成 ^5.1.0
  3. 刪掉 ts-helpers
  4. zone.js 版本號更新至 ^0.8.4

devDependencies 段落中的:

  1. @angular/compiler-cli 的版本改成 ^4.0.0
  2. @types/node 版本改成 ~6.0.60
  3. codelyzer 版本改成 ~2.0.0
  4. jasmine-core 版本號更新至 ~2.5.2
  5. jasmine-spec-reporter 版本號更新至 ~3.2.0
  6. karma 版本號更新至 ~1.4.1
  7. karma-chrome-launcher 版本號更新至 ~2.0.0
  8. karma-cli 版本號更新至 ~1.0.1
  9. karma-jasmine 版本號更新至 ~1.1.0
  10. 新增一行 "karma-jasmine-html-reporter": "^0.2.2",
  11. 新增一行 "karma-coverage-istanbul-reporter": "^0.2.0",
  12. 刪除 karma-remap-istanbul
  13. protractor 版本號更新至 ~5.1.0
  14. ts-node 版本號更新至 ~2.0.0
  15. tslint 版本號更新至 ~4.5.0
  16. typescript 版本號更新至 ~2.1.0

最後更新 scripts 為:

"scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },複製程式碼

更新 kama.conf.js

src/kama.conf.js 改成如下:

// Karma configuration file, see link for more information
// https://karma-runner.github.io/0.13/config/configuration-file.html

module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', '@angular/cli'],
    plugins: [
      require('karma-jasmine-html-reporter'),
      require('karma-coverage-istanbul-reporter'),
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('@angular/cli/plugins/karma')
    ],
    client:{
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    files: [
      { pattern: './src/test.ts', watched: false }
    ],
    preprocessors: {
      './src/test.ts': ['@angular/cli']
    },
    mime: {
      'text/x-typescript': ['ts','tsx']
    },
    coverageIstanbulReporter: {
      reports: [ 'html', 'lcovonly' ],
      fixWebpackSourcePaths: true
    },
    reporters: config.angularCli && config.angularCli.codeCoverage
          ? ['progress', 'coverage-istanbul']
          : ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  });
};複製程式碼

更新 protractor.conf.js

src/protractor.conf.js 改成如下

// Protractor configuration file, see link for more information
// https://github.com/angular/protractor/blob/master/docs/referenceConf.js

/*global jasmine */
const { SpecReporter } = require('jasmine-spec-reporter');

exports.config = {
  allScriptsTimeout: 11000,
  specs: [
    './e2e/**/*.e2e-spec.ts'
  ],
  capabilities: {
    'browserName': 'chrome'
  },
  directConnect: true,
  baseUrl: 'http://localhost:4200/',
  framework: 'jasmine',
  jasmineNodeOpts: {
    showColors: true,
    defaultTimeoutInterval: 30000,
    print: function() {}
  },
  beforeLaunch: function() {
    require('ts-node').register({
      project: 'e2e'
    });
  },
  onPrepare: function() {
    jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
  }
};複製程式碼

更新 tslint.json

搜尋 no-inferrable-types 把這行改成:

"no-inferrable-types": [true, "ignore-params"],複製程式碼

然後新增下面的規則:

"callable-types": true,
"import-blacklist": [true, "rxjs"],
"import-spacing": true,
"interface-over-type-literal": true,
"no-empty-interface": true,
"no-string-throw": true,
"prefer-const": true,
"typeof-compare": true,
"unified-signatures": true,複製程式碼

重新安裝依賴軟體包

接下來需要重新安裝依賴項:

rm -rf node_modules dist # Windows 使用者使用 rmdir 來刪除
npm install --save-dev @angular/cli@latest
npm install複製程式碼

到此為止,升級結束,執行 ng serveng build 一切正常。我目前只升級了第一章程式碼,計劃會在 ng4tut 陸續把教程程式碼都升級到 4.x

4.0 程式碼地址:
github.com/wpcfan/awes…

2.x 程式碼地址:

github.com/wpcfan/awes…

相關文章