# 手寫組件
以往的組件我們都是使用的angular cli來自動生成,本節我們手動創建一個:
## Welcome組件
我們需要一個歡迎組件來對登錄成功后的用戶顯示歡迎信息,為此在`src/app`文件夾中創建`welcome.component.ts`文件:
```bash
panjiedeMacBook-Pro:app panjie$ pwd
/Users/panjie/github/mengyunzhi/angular11-guild/first-app/src/app
panjiedeMacBook-Pro:app panjie$ tree -L 1
.
├── add
├── app-routing.module.ts
├── app.component.css
├── app.component.html
├── app.component.spec.ts
├── app.component.ts
├── app.module.ts
├── edit
├── entity
├── index
├── login
├── personal-center
├── welcome.component.ts ??
├── x-auth-token.interceptor.spec.ts
└── x-auth-token.interceptor.ts
6 directories, 9 files
```
接著打開該文件,簡單寫些組件初始化的注釋:
```typescript
+++ b/first-app/src/app/welcome.component.ts
@@ -0,0 +1,6 @@
+// 定義類
+// 使用注解來標明它是一個組件
+// 將組件加入模塊
+// 為組件設置一個selector,以使得其它組件能夠使用它
+// 為組件設置V層模板
+// 新建個測試文件來測試組件
```
接下來分步完成任務:
## 定義類
```typescript
+++ b/first-app/src/app/welcome.component.ts
@@ -1,4 +1,7 @@
// 定義類
+export class WelcomeComponent {
+
+}
// 使用注解來標明它是一個組件
// 將組件加入模塊
// 為組件設置一個selector,以使得其它組件能夠使用它
```
此時應該注意兩點:
- 類名應該與文件名保持一致,且以`Component`結尾。
- 必須加入`export`以使該類能夠被其它文件引用
## 加注解
```typescript
+++ b/first-app/src/app/welcome.component.ts
@@ -1,8 +1,13 @@
// 定義類
+import {Component} from '@angular/core';
+
+// 使用注解來標明它是一個組件
+@Component({
+
+})
export class WelcomeComponent {
}
-// 使用注解來標明它是一個組件
// 將組件加入模塊
// 為組件設置一個selector,以使得其它組件能夠使用它
// 為組件設置V層模板
```
`@Component`位于`'@angular/core'`中,其接收的參數類型為**對象**。
注解是注釋的一種延伸,使用`@`關鍵字打頭。相對于普通的注釋,注解不但可以起到對類、屬性、方法等的說明作用,而且可以為其設置一些屬性或其它更高級的做法。在Angular中,某個類是一個組件還是一個模塊,都是由其類上對應的注解實現的。
比如我們在此使用`@Component`將`WelcomeComponent`類聲明為一個Angular組件。如果沒有此注解,則`WelcomeComponent`就是一個普普通通的類,如果我們在此使用`@Module`注解,則`WelcomeComponent`類便成為了Angular的一個模塊。
除了`@Component`、`@Module`注解外,我們前面還接觸了將類聲明為管道的注解`@Pipe`,將類型的某個屬性聲明為可對接父組件方法的注解`@Output()`。
## 加入到模塊
```typescript
+++ b/first-app/src/app/app.module.ts
@@ -13,6 +13,7 @@ import {IndexComponent} from './index/index.component';
import { PersonalCenterComponent } from './personal-center/personal-center.component';
import { SexPipe } from './personal-center/sex.pipe';
import {XAuthTokenInterceptor} from './x-auth-token.interceptor';
+import {WelcomeComponent} from './welcome.component';
@NgModule({
@@ -23,7 +24,8 @@ import {XAuthTokenInterceptor} from './x-auth-token.interceptor';
LoginComponent,
IndexComponent,
PersonalCenterComponent,
- SexPipe
+ SexPipe,
+ WelcomeComponent
],
imports: [
BrowserModule,
```
組件依賴于模塊,沒有模塊的組件是沒有意義的。
## 定義selector
```typescript
+++ b/first-app/src/app/welcome.component.ts
@@ -3,7 +3,7 @@ import {Component} from '@angular/core';
// 使用注解來標明它是一個組件
@Component({
-
+ selector: 'app-welcome'
})
export class WelcomeComponent {
```
使用`selector`來定義關鍵字,其它的組件在V層中使用`app-welcome`來加載該組件。
## 定義V層
V層可以定義一個單獨的文件,也可以直接寫在C層中:
```typescript
+++ b/first-app/src/app/welcome.component.ts
@@ -3,7 +3,8 @@ import {Component} from '@angular/core';
// 使用注解來標明它是一個組件
@Component({
-
+ selector: 'app-welcome',
+ template: `<h1 class="text-center">歡迎使用由軟小白開發的教務管理系統</h1>`
})
export class WelcomeComponent {
```
- 直接定義模板時使用`template`.
- 定義模板文件時使用`templateUrl`
- 可以不定義樣式文件或樣式
## 測試
手動新建文件welcome.component.spec.ts
```bash
panjiedeMacBook-Pro:app panjie$ pwd
/Users/panjie/github/mengyunzhi/angular11-guild/first-app/src/app
panjiedeMacBook-Pro:app panjie$ tree -L 1
.
├── add
├── app-routing.module.ts
├── app.component.css
├── app.component.html
├── app.component.spec.ts
├── app.component.ts
├── app.module.ts
├── edit
├── entity
├── index
├── login
├── personal-center
├── welcome.component.spec.ts ??
├── welcome.component.ts
├── x-auth-token.interceptor.spec.ts
└── x-auth-token.interceptor.ts
6 directories, 10 files
```
接下來,我們手動一步步的寫一個測試方法,從而更清晰的明了測試文件代碼的構成。
### 增加測試方法`describe`
測試方法需要寫在`describe('對測試的描述', 回調函數)`中,所以我們需要在測試文件中首先調用`describe`方法。
```typescript
+++ b/first-app/src/app/welcome.component.spec.ts
@@ -0,0 +1,3 @@
+describe('welcome', () => {
+
+});
```
### 增加測試用例`fit`
執行的測試用例應寫到`it('測試用例名稱', 具體要執行的回調函數中)`,我們在此要測試Welcome組件是否可以初始化成功,則接著應該添加`it`方法。
```typescript
+++ b/first-app/src/app/welcome.component.spec.ts
@@ -1,3 +1,5 @@
describe('welcome', () => {
+ fit('welcome create', () => {
+ });
});
```
### 運行測試
使用`ng t`啟動項目,并查看是否運行:

如上代碼表明,測試用例**welcome create**成功執行,但該方法中沒有任何斷言。按單元測試的邏輯,我們應該在單元測試的代碼以代碼的形式來保證代碼的正確性,在單元測試中,我們使用`expect()`關鍵字,比如我們斷言`1+1`應該竺于`2`:
```typescript
+++ b/first-app/src/app/welcome.component.spec.ts
@@ -1,5 +1,5 @@
describe('welcome', () => {
fit('welcome create', () => {
-
+ expect(1 + 1).toBe(2);
});
});
```
此時`expect`中的值與`toBe`中的值相同,則表達斷言通過。表明`expect`中的代碼執行結果是符合預期的。

## 創建動態測試模塊
在Angular的單元測試中,動態測試模塊運行在一個叫`TestBed`測試機床中。它`TestBed`提供了配置動態模塊的相關方法:
```typescript
+++ b/first-app/src/app/welcome.component.spec.ts
@@ -1,5 +1,10 @@
+import {TestBed} from '@angular/core/testing';
+
describe('welcome', () => {
fit('welcome create', () => {
- expect(1 + 1).toBe(2);
+ // 配置動態測試模塊
+ TestBed.configureTestingModule({
+
+ });
});
});
```
預測試Welcome組件,則需要將其聲明為動態測試模塊的一部分:
```typescript
+++ b/first-app/src/app/welcome.component.spec.ts
@@ -1,10 +1,11 @@
import {TestBed} from '@angular/core/testing';
+import {WelcomeComponent} from './welcome.component';
describe('welcome', () => {
fit('welcome create', () => {
// 配置動態測試模塊
TestBed.configureTestingModule({
-
+ declarations: [WelcomeComponent]
});
});
});
```
進模塊進行配置后,通過調用其`compileComponents`完成對該模塊中的組件的編譯(可以理解為C語言中的編譯,即把一種形式轉換為另一種形式)工作。
```typescript
+++ b/first-app/src/app/welcome.component.spec.ts
@@ -6,6 +6,6 @@ describe('welcome', () => {
// 配置動態測試模塊
TestBed.configureTestingModule({
declarations: [WelcomeComponent]
- });
+ }).compileComponents();
});
});
```
隨后便可以調用`TestBed`提供的 來獲取一個組件實例了:
```typescript
+++ b/first-app/src/app/welcome.component.spec.ts
@@ -7,5 +7,8 @@ describe('welcome', () => {
TestBed.configureTestingModule({
declarations: [WelcomeComponent]
}).compileComponents();
+
+ const welcomeComponent = TestBed.createComponent(WelcomeComponent);
+ expect(welcomeComponent).toBeTruthy();
});
});
```
測試結果如下:

而至于使用angular cli自動創建的測試文件為什么與我們手寫的不同,待后面有機會再共同學習。
## 完善組件
最后,我們為歡迎文字添加點顏色,并為其添加一個上邊距,刪除相關冗余的注釋:
```typescript
+++ b/first-app/src/app/welcome.component.ts
@@ -4,12 +4,9 @@ import {Component} from '@angular/core';
// 使用注解來標明它是一個組件
@Component({
selector: 'app-welcome',
- template: `<h1 class="text-center">歡迎使用由軟小白開發的教務管理系統</h1>`
+ template: `<h1 class="text-center text-success mt-5 pt-5">
+ 歡迎使用由軟小白開發的教務管理系統</h1>`
})
export class WelcomeComponent {
}
-// 將組件加入模塊
-// 為組件設置一個selector,以使得其它組件能夠使用它
-// 為組件設置V層模板
-// 新建個測試文件來測試組件
```

| 名稱 | 地址 |
| --------------- | ------------------------------------------------------------ |
| Angular組件概述 | [https://angular.cn/guide/component-overview#creating-a-component-manually](https://angular.cn/guide/component-overview#creating-a-component-manually) |
| TestBed | [https://angular.cn/api/core/testing/TestBed](https://angular.cn/api/core/testing/TestBed) |
| 本節源碼 | [https://github.com/mengyunzhi/angular11-guild/archive/step5.2.zip](https://github.com/mengyunzhi/angular11-guild/archive/step5.2.zip) |
- 序言
- 第一章 Hello World
- 1.1 環境安裝
- 1.2 Hello Angular
- 1.3 Hello World!
- 第二章 教師管理
- 2.1 教師列表
- 2.1.1 初始化原型
- 2.1.2 組件生命周期之初始化
- 2.1.3 ngFor
- 2.1.4 ngIf、ngTemplate
- 2.1.5 引用 Bootstrap
- 2.2 請求后臺數據
- 2.2.1 HttpClient
- 2.2.2 請求數據
- 2.2.3 模塊與依賴注入
- 2.2.4 異步與回調函數
- 2.2.5 集成測試
- 2.2.6 本章小節
- 2.3 新增教師
- 2.3.1 組件初始化
- 2.3.2 [(ngModel)]
- 2.3.3 對接后臺
- 2.3.4 路由
- 2.4 編輯教師
- 2.4.1 組件初始化
- 2.4.2 獲取路由參數
- 2.4.3 插值與模板表達式
- 2.4.4 初識泛型
- 2.4.5 更新教師
- 2.4.6 測試中的路由
- 2.5 刪除教師
- 2.6 收尾工作
- 2.6.1 RouterLink
- 2.6.2 fontawesome圖標庫
- 2.6.3 firefox
- 2.7 總結
- 第三章 用戶登錄
- 3.1 初識單元測試
- 3.2 http概述
- 3.3 Basic access authentication
- 3.4 著陸組件
- 3.5 @Output
- 3.6 TypeScript 類
- 3.7 瀏覽器緩存
- 3.8 總結
- 第四章 個人中心
- 4.1 原型
- 4.2 管道
- 4.3 對接后臺
- 4.4 x-auth-token認證
- 4.5 攔截器
- 4.6 小結
- 第五章 系統菜單
- 5.1 延遲及測試
- 5.2 手動創建組件
- 5.3 隱藏測試信息
- 5.4 規劃路由
- 5.5 定義菜單
- 5.6 注銷
- 5.7 小結
- 第六章 班級管理
- 6.1 新增班級
- 6.1.1 組件初始化
- 6.1.2 MockApi 新建班級
- 6.1.3 ApiInterceptor
- 6.1.4 數據驗證
- 6.1.5 教師選擇列表
- 6.1.6 MockApi 教師列表
- 6.1.7 代碼重構
- 6.1.8 小結
- 6.2 教師列表組件
- 6.2.1 初始化
- 6.2.2 響應式表單
- 6.2.3 getTestScheduler()
- 6.2.4 應用組件
- 6.2.5 小結
- 6.3 班級列表
- 6.3.1 原型設計
- 6.3.2 初始化分頁
- 6.3.3 MockApi
- 6.3.4 靜態分頁
- 6.3.5 動態分頁
- 6.3.6 @Input()
- 6.4 編輯班級
- 6.4.1 測試模塊
- 6.4.2 響應式表單驗證
- 6.4.3 @Input()
- 6.4.4 FormGroup
- 6.4.5 自定義FormControl
- 6.4.6 代碼重構
- 6.4.7 小結
- 6.5 刪除班級
- 6.6 集成測試
- 6.6.1 惰性加載
- 6.6.2 API攔截器
- 6.6.3 路由與跳轉
- 6.6.4 ngStyle
- 6.7 初識Service
- 6.7.1 catchError
- 6.7.2 單例服務
- 6.7.3 單元測試
- 6.8 小結
- 第七章 學生管理
- 7.1 班級列表組件
- 7.2 新增學生
- 7.2.1 exports
- 7.2.2 自定義驗證器
- 7.2.3 異步驗證器
- 7.2.4 再識DI
- 7.2.5 屬性型指令
- 7.2.6 完成功能
- 7.2.7 小結
- 7.3 單元測試進階
- 7.4 學生列表
- 7.4.1 JSON對象與對象
- 7.4.2 單元測試
- 7.4.3 分頁模塊
- 7.4.4 子組件測試
- 7.4.5 重構分頁
- 7.5 刪除學生
- 7.5.1 第三方dialog
- 7.5.2 批量刪除
- 7.5.3 面向對象
- 7.6 集成測試
- 7.7 編輯學生
- 7.7.1 初始化
- 7.7.2 自定義provider
- 7.7.3 更新學生
- 7.7.4 集成測試
- 7.7.5 可訂閱的路由參數
- 7.7.6 小結
- 7.8 總結
- 第八章 其它
- 8.1 打包構建
- 8.2 發布部署
- 第九章 總結