天天看點

angular7更新到angular8

最近換了項目組,技術棧也更換了(原來是Vue,現在是angular)。正好趕上版本更新,于是自己嘗試更新,順便記錄了過程中遇到的問題,希望可以幫到需要更新的朋友。

  1. 先使用

    ng update

    檢視我們這個項目需要更新的依賴
    angular7更新到angular8
  2. 依次更新對應的依賴(根據上圖中

    Command to update

    的提示一次更新)
ng update @angular/cli
           
  1. 更新後踩過的坑(問題下面的解決方案,隻是寫了一個示例)

    可以通過

    ng lint

    指令,檢視需要修改的檔案清單,根據提示一一修改。

    也可以先通過

    ng lint --fix

    指令,先自動修複。然後再看需要手動修複的錯誤。

    (1)

    ViewChild

    ContentChild

    用法發生了變化
Before:
		@ViewChild('foo') foo: ElementRef;
After:
		@ViewChild('foo', {static: true}) foo: ElementRef;
           

(2)

Boolean ===》 boolean

問題:

Type boolean trivially inferred from a boolean literal, remove type annotation

@output showAllTab :boolean = false;

改為

@output showAllTab = false;

(3) 問題:

Consecutive blank lines are forbidden

禁止連續空行

(4) 問題:

Don't use 'String' as a type. Avoid using the

String

type. Did you mean

string

?

name: String;

改成

name: string;

(5) 問題:

Don't use 'Number' as a type. Avoid using the

Number

type. Did you mean

number

?

categoryId: Number;

改為

categoryId: number;

(6) 問題:

Don't use 'Object' as a type. Avoid using the

Object

type. Did you mean

object

?

totalBillList: Object = new TotalBillModel();

改為

totalBillList: object = new TotalBillModel();

(7) 問題:

Unnecessarily quoted property 'username' found.

‘username’: username

改為

username: username

(8) 問題:

Spaces before function parens are disallowed

formatter: function (value) {
	return '$' + value;
}
           

改為

formatter: value => {
	return '$' + value;
}
           

(9) 問題:

Type number trivially inferred from a number literal, remove type annotation

有初始值的話 不需要定義類型

selectType: number = 0;

改為

selectType= 0;

(10) 問題:

Multiple variable declarations in the same statement are forbidden

多元變量禁止在同一條語句中聲明

const input = control.value, isValid = input < min;

改為

const input = control.value;
 const isValid = input < min;
           

(11) 問題:

statements are not aligned

語句未對齊

(12) 問題:

file should end with a newline。

檔案應以換行符結尾