angular 資料類型
- 布林類型(true/false)
isCar: boolean = true;
console.log(this.isCar);
//輸出結果: true
- 數字類型
num1: number = 100;
num2: number = 50;
console.log(this.num1+this.num2);
//輸出結果: 150
- 字串類型
str1: string = ‘Hello’;
str2: string = ‘Angular’;
console.log(this.str1 + ‘ ‘ + this.str2);
//輸出結果: Hello Angular
- 陣列類型
arr1: number[] = [1,2,3];
arr2: Array<number> = [1,2,3];
arr3: [string, number] = [‘Hello’, 123];
console.log(this.arr1[0]);
console.log(this.arr2[1]);
console.log(this.arr3[0]);
//輸出結果: 1
//輸出結果: 2
//輸出結果: Hello
- 列舉類型(預設從0開始)
enum Color {Red = 1, Green, Blue};
console.log(‘Red=’ + Color.Red);
console.log(‘Green=’ + Color.Green);
console.log(‘Blue=’ + Color.Blue);
//輸出結果: Red=1 Green=2 Blue=3
enum Color {Red = 2, Green=4, Blue=6};
console.log(‘Red=’ + Color.Red);
console.log(‘Green=’ + Color.Green);
console.log(‘Blue=’ + Color.Blue);
//輸出結果: Red=2 Green=4 Blue=6