# 着陆组件 在一个网站中,我们把首页又习惯性的称为着陆页。这是因为大多数时候首页是用户第一个访问的页面。在Angular中也有类似的设置,我把它称为着陆组件,指系统启动时第一个启动的组件。在前面的章节中,我们对模块进行了如下剖析: ![image-20210228173021966](https://img.kancloud.cn/c6/77/c6778e95733b9e82f3a492baa4baeb92_1410x542.png) 其中的`bootstrap`中的元素被标注为`启动组件`,以当前`AppModule`为例相关代码如下: ```typescript bootstrap: [AppComponent] }) export class AppModule { } ``` 接下来,我们共同学习在Angular中如何自定义一个启动组件。 ## 自定义启动组件 项目的`src`文件中有如下文件: ```bash panjiedeMacBook-Pro:src panjie$ pwd /Users/panjie/github/mengyunzhi/angular11-guild/first-app/src panjiedeMacBook-Pro:src panjie$ tree -L 1 . ├── app ├── assets ├── environments ├── favicon.ico ├── index.html ├── main.ts ├── polyfills.ts ├── styles.css └── test.ts 3 directories, 6 files ``` 这其中的`main.ts`以`index.html`共同决定了当前项目的启动组件。我们知道组件依存于模块,一个没有模块的组件是没法被启动的。 ![image-20210228172606027](https://img.kancloud.cn/14/de/14de94b8e79938eafff7deacb1db77f0_1448x502.png) ### 启动模块 那么如若指定某个着陆组件,则必然先指定着陆组件所在的模块,该过程是由`main.ts`来完成的: ```typescript platformBrowserDynamic().bootstrapModule(AppModule) 👈 .catch(err => console.error(err)); ``` 在`bootstrapModule`方法中指定了启动模块`AppModule`。 ### 启动组件 然后在`index.html`指定了启动的组件: ```html <body> <app-root></app-root> 👈 </body> ``` 只有在启动模块中被声明为启动组件的组件,才有资格(而且是必须)出现在这里。 `<app-root>`对应的便是AppComponent: ```typescript @Component({ selector: 'app-root', 👈 templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent implements OnInit { ``` ## 新建着陆组件 我们新建一个index组件做为着陆组件使用,以期在该组件中实现:如果未登录则显示登录组件,如果已登录则显示教师列表。 ```bash panjiedeMacBook-Pro:app panjie$ pwd /Users/panjie/github/mengyunzhi/angular11-guild/first-app/src/app panjiedeMacBook-Pro:app panjie$ ng g c index CREATE src/app/index/index.component.css (0 bytes) CREATE src/app/index/index.component.html (20 bytes) CREATE src/app/index/index.component.spec.ts (619 bytes) CREATE src/app/index/index.component.ts (271 bytes) UPDATE src/app/app.module.ts (884 bytes) ``` 然后做两项工作: 将index组件声明为启动组件: ```typescript +++ b/first-app/src/app/app.module.ts @@ -28,7 +28,7 @@ import {IndexComponent} from './index/index.component'; RouterModule ], providers: [], - bootstrap: [AppComponent] + bootstrap: [IndexComponent] }) export class AppModule { } ``` 并在index.html中引用它: ```html +++ b/first-app/src/index.html @@ -8,6 +8,6 @@ <link rel="icon" type="image/x-icon" href="favicon.ico"> </head> <body> - <app-root></app-root> + <app-index></app-index> </body> </html> ``` ### 测试 最后使用`ng s`来测试下是否达到了预期效果。 ![image-20210305110928880](https://img.kancloud.cn/15/55/15557ba2bbd276b2151ddadbdbad16f6_988x208.png) It works! ## 登录功能 接下来,我们将登录组件、教师列表组件加入到着陆组件,并实现:登录前显示登录组件,登录后显示教师列表组件。 ### 初始化 在V层中增加两个组件,一个在登录前显示,一个在登录成功后显示: ```html +++ b/first-app/src/app/index/index.component.html @@ -1 +1,2 @@ -<p>index works!</p> +<app-root *ngIf="login"></app-root> +<app-login *ngIf="!login" ></app-login> ``` C层中初始化login变量: ```typescript +++ b/first-app/src/app/index/index.component.ts @@ -7,6 +7,8 @@ import {Component, OnInit} from '@angular/core'; }) export class IndexComponent implements OnInit { + login = false; + constructor() { } ``` 移动历史的`fdescribe`以及`fit`,使用`ng t`来启动index组件,将得到如下错误信息: ![image-20210305112058533](https://img.kancloud.cn/4e/2e/4e2e264b09aeb2dd59a6606cfc75b53a_1950x120.png) 这是由于当前动态测试模块中只声明了Index组件,所以它是无法解析`<app-root>`以及`<app-login>`两个html元素的。若使动态测试模块拥有`<app-root>`以及`<app-login>`的能力,仅仅需要在其模块中声明其拥有这两个组件即可: ```typescript +++ b/first-app/src/app/index/index.component.spec.ts @@ -1,6 +1,8 @@ import {ComponentFixture, TestBed} from '@angular/core/testing'; import {IndexComponent} from './index.component'; +import {AppComponent} from '../app.component'; +import {LoginComponent} from '../login/login.component'; describe('IndexComponent', () => { let component: IndexComponent; @@ -8,7 +10,7 @@ describe('IndexComponent', () => { beforeEach(async () => { await TestBed.configureTestingModule({ - declarations: [IndexComponent] + declarations: [IndexComponent, AppComponent, LoginComponent] }) .compileComponents(); }); ``` 错误重新显示为: ![image-20210305112359394](https://img.kancloud.cn/ad/b5/adb5530ea68fda7af4a62375feaf391c_1562x168.png) 使当前模块拥有提供HttpClient的能力,则需要引用拥有提供HttpClient能力的HttpClientModule: ```typescript +++ b/first-app/src/app/index/index.component.spec.ts @@ -3,6 +3,7 @@ import {ComponentFixture, TestBed} from '@angular/core/testing'; import {IndexComponent} from './index.component'; import {AppComponent} from '../app.component'; import {LoginComponent} from '../login/login.component'; +import {HttpClientModule} from '@angular/common/http'; describe('IndexComponent', () => { let component: IndexComponent; @@ -10,7 +11,8 @@ describe('IndexComponent', () => { beforeEach(async () => { await TestBed.configureTestingModule({ - declarations: [IndexComponent, AppComponent, LoginComponent] + declarations: [IndexComponent, AppComponent, LoginComponent], + imports: [HttpClientModule] }) .compileComponents(); }); ``` ![image-20210305112553206](https://img.kancloud.cn/ed/ca/edca4b9f97aaaaaeacf574f00cae6c66_1282x118.png) 继续引用能够解析`ngModel`指令的`FormsModule`模块: ```typescript +++ b/first-app/src/app/index/index.component.spec.ts @@ -4,6 +4,7 @@ import {IndexComponent} from './index.component'; import {AppComponent} from '../app.component'; import {LoginComponent} from '../login/login.component'; import {HttpClientModule} from '@angular/common/http'; +import {FormsModule} from '@angular/forms'; describe('IndexComponent', () => { let component: IndexComponent; @@ -12,7 +13,7 @@ describe('IndexComponent', () => { beforeEach(async () => { await TestBed.configureTestingModule({ declarations: [IndexComponent, AppComponent, LoginComponent], - imports: [HttpClientModule] + imports: [HttpClientModule, FormsModule] }) .compileComponents(); }); ``` 最后错误消失,成功启动组件并显示登录组件。 ![image-20210305120532468](https://img.kancloud.cn/21/59/21599a3985132629c061049cc7c545a4_1224x534.png) 最后,做为小白的我们在使用`ng t`时最为关键的一步,加入自动变更检测相关的代码: ```typescript +++ b/first-app/src/app/index/index.component.spec.ts @@ -26,5 +26,6 @@ describe('IndexComponent', () => { fit('should create', () => { expect(component).toBeTruthy(); + fixture.autoDetectChanges(); }); }); ``` ## 父子组件 当下我们有三个组件:做为父组件的Index,以及做为子组件Login、App。若要使登录子组件将登录成功的通知发送给其父组件Index,则可以向Login中注入父组件: ```typescript +++ b/first-app/src/app/login/login.component.ts @@ -1,5 +1,6 @@ import {Component, OnInit} from '@angular/core'; import {HttpClient, HttpHeaders} from '@angular/common/http'; +import {IndexComponent} from '../index/index.component'; @Component({ selector: 'app-login', @@ -12,7 +13,8 @@ export class LoginComponent implements OnInit { password: string }; - constructor(private httpClient: HttpClient) { + constructor(private httpClient: HttpClient, + private indexComponent: IndexComponent) { } ngOnInit(): void { @@ -31,7 +33,7 @@ export class LoginComponent implements OnInit { .get( 'http://angular.api.codedemo.club:81/teacher/login', {headers: httpHeaders}) - .subscribe(teacher => console.log(teacher), + .subscribe(teacher => this.indexComponent.login = true, error => console.log('发生错误, 登录失败', error)); } } ``` 登录成功后,将Index组件中的login属性设置为true。此时在输入正确的用户名密码并点击登录后,将成功的显示教师列表组件: ![image-20210305121257833](https://img.kancloud.cn/0a/7b/0a7b7f1208bab18e46c08a1e5692296d_1416x542.png) 👇 ![image-20210305121333839](https://img.kancloud.cn/0e/a2/0ea2f865f7fc06074f606dc60eb72118_1520x514.png) 登录功能成功实现,现在我们可以使用`ng s`来运行项目,并查看在其中的表现了。 ## 本节作业 登录成功后,在控制中将会发生一个异常。你知道此异常产生的原因吗?如果知道,那么尝试把它解决掉。 | 名称 | 地址 | 备注 | | -------- | ------------------------------------------------------------ | ---- | | 本节源码 | [https://github.com/mengyunzhi/angular11-guild/archive/step3.4.zip](https://github.com/mengyunzhi/angular11-guild/archive/step3.4.zip) | |