本節我們共同來完成學生列表組件。
## 新建組件
我們來到`src/app/stduent`文件中,執行`ng g c student --flat`來創建學生列表組件:
```bash
panjie@panjies-iMac student % pwd
/Users/panjie/github/mengyunzhi/angular11-guild/first-app/src/app/student
panjie@panjies-iMac student % ng g c student --flat
CREATE src/app/student/student.component.css (0 bytes)
CREATE src/app/student/student.component.html (22 bytes)
CREATE src/app/student/student.component.spec.ts (633 bytes)
CREATE src/app/student/student.component.ts (279 bytes)
UPDATE src/app/student/student.module.ts (401 bytes)
```
教程中我們使用了太多遍的`ng g c xx`命令來創建組件,在創建過程中也使用過`-m`參數來指定組件模塊,剛剛又使用了`--flat`參數(否則將創建student子文件夾)在當前文件夾中創建了組件。那么問題來了,我又是怎么知道這些參數的呢?答案很簡單:幫助文檔。
基本上每個命令都會有幫助文檔,在`shell`中往往可以通過` --help`參數來獲取到它們。如果這些命令必須加一些參數才能正常工作,則直接輸入這個命令也可能得到一些幫助信息;如果這些命令是操作系統原生的,在`xunix`系統中還可以通過`man 命令名`來獲取幫助文檔。
`ng`是Angular cli提供的命令,可以使用`ng`來獲取到幫助文檔:
```bash
panjie@panjies-iMac app % ng
Available Commands:
add Adds support for an external library to your project.
analytics Configures the gathering of Angular CLI usage metrics. See https://angular.io/cli/usage-analytics-gathering.
build (b) Compiles an Angular app into an output directory named dist/ at the given output path. Must be executed from within a workspace directory.
deploy Invokes the deploy builder for a specified project or for the default project in the workspace.
config Retrieves or sets Angular configuration values in the angular.json file for the workspace.
doc (d) Opens the official Angular documentation (angular.io) in a browser, and searches for a given keyword.
e2e (e) Builds and serves an Angular app, then runs end-to-end tests using Protractor.
extract-i18n (i18n-extract, xi18n) Extracts i18n messages from source code.
generate (g) Generates and/or modifies files based on a schematic.
help Lists available commands and their short descriptions.
lint (l) Runs linting tools on Angular app code in a given project folder.
new (n) Creates a new workspace and an initial Angular application.
run Runs an Architect target with an optional custom builder configuration defined in your project.
serve (s) Builds and serves your app, rebuilding on file changes.
test (t) Runs unit tests in a project.
update Updates your application and its dependencies. See https://update.angular.io/
version (v) Outputs Angular CLI version.
For more detailed help run "ng [command name] --help"
```
在學習過程中出現英文的提示新手往往就是一笑而過,完全不管開發者具體說了些什么。這是每個非英語為母語的程序員都必須改掉的壞毛病。試想下如果我們做為開發者,同樣希望把重要的信息以文字的開工傳遞給使用者,如果我們開發的項目想被全球的程序員使用,則這個語言必然是英文的。以筆者多年的教學、開發經驗來看,有絕大多數的問題都可以在提示信息中輕構的找到問題的根本原因,從而幫助我們快速的找到答案。
再比如我們還可以通過輸入`ng g c --help`來看到生成組件時支持的參數,而教程中使用過的`-m`以及`--plat`參數則全部來源于該幫助文檔。
## 初始化
組件的初始化需要C、V的配合,在實際的開發過程中往往在先V后C,邊V邊C。 當在V層中需要什么屬性性,便隨時在C層中添加什么屬性。
```html
<div class="row">
<div class="col-12 text-right">
<a class="btn btn-primary mr-2" routerLink="./add"><i class="fas fa-plus"></i>新增</a>
</div>
</div>
<table class="table table-striped mt-2">
<thead>
<tr class="table-primary">
<th>序號</th>
<th>姓名</th>
<th>學號</th>
<th>手機號</th>
<th>班級</th>
<th>班主任</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let student of pageData.content; index as index">
<td>{{index + 1}}</td>
<td>{{student.name}}</td>
<td>{{student.number}}</td>
<td>{{student.phone}}</td>
<td>{{student.clazz.name}}</td>
<td>{{student.clazz.teacher.name}}</td>
<td>
<a class="btn btn-outline-primary btn-sm" routerLink="edit/{{student.id}}">
<i class="fas fa-pen"></i>編輯
</a>
<span class="btn btn-sm btn-outline-danger" (click)="onDelete(index, student.id)">
<i class="far fa-trash-alt"></i>刪除
</span>
</td>
</tr>
</tbody>
</table>
```
對應C層文件:
```typescript
import {Component, OnInit} from '@angular/core';
import {Page} from '../entity/page';
import {Student} from '../entity/student';
@Component({
selector: 'app-student',
templateUrl: './student.component.html',
styleUrls: ['./student.component.css']
})
export class StudentComponent implements OnInit {
pageData = {} as Page<Student>;
constructor() {
}
ngOnInit(): void {
}
onDelete(index: number, id: number): void {
}
}
```
在V層中使用到了路由模塊的相關功能,所以在單元測試中加入路由測試模塊:
```typescript
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {StudentComponent} from './student.component';
import {RouterTestingModule} from '@angular/router/testing';
describe('StudentComponent', () => {
let component: StudentComponent;
let fixture: ComponentFixture<StudentComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [StudentComponent],
imports: [
RouterTestingModule
]
})
.compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(StudentComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
fit('should create', () => {
expect(component).toBeTruthy();
});
});
```
最終效果如下:

| 名稱 | 鏈接 |
| -------- | ------------------------------------------------------------ |
| 本節源碼 | [https://github.com/mengyunzhi/angular11-guild/archive/step7.4.zip](https://github.com/mengyunzhi/angular11-guild/archive/step7.4.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 發布部署
- 第九章 總結