组件通讯

本烹饪宝典包含了常见的组件通讯场景,也就是让两个或多个组件之间共享信息的方法。

要深入了解组件通讯的各个基本概念,在组件通讯文档中可以找到详细的描述和例子。

目录

Contents

参见live example / downloadable example

通过输入型绑定把数据从父组件传到子组件。

HeroChildComponent 有两个输入型属性,它们通常带@Input装饰器

  1. import { Component, Input } from '@angular/core';
  2. import { Hero } from './hero';
  3. @Component({
  4. selector: 'hero-child',
  5. template: `
  6. <h3>{{hero.name}} says:</h3>
  7. <p>I, {{hero.name}}, am at your service, {{masterName}}.</p>
  8. `
  9. })
  10. export class HeroChildComponent {
  11. @Input() hero: Hero;
  12. @Input('master') masterName: string;
  13. }

第二个@Input为子组件的属性名masterName指定一个别名master(译者注:不推荐为起别名,请参见风格指南).

父组件HeroParentComponent把子组件的HeroChildComponent放到*ngFor循环器中,把自己的master字符串属性绑定到子组件的master别名上,并把每个循环的hero实例绑定到子组件的hero属性。

  1. import { Component } from '@angular/core';
  2. import { HEROES } from './hero';
  3. @Component({
  4. selector: 'hero-parent',
  5. template: `
  6. <h2>{{master}} controls {{heroes.length}} heroes</h2>
  7. <hero-child *ngFor="let hero of heroes"
  8. [hero]="hero"
  9. [master]="master">
  10. </hero-child>
  11. `
  12. })
  13. export class HeroParentComponent {
  14. heroes = HEROES;
  15. master: string = 'Master';
  16. }

运行应用程序会显示三个英雄:

Parent-to-child

测试

端到端测试,用于确保所有的子组件都像所期待的那样被初始化并显示出来。

  1. // ...
  2. let _heroNames = ['Mr. IQ', 'Magneta', 'Bombasto'];
  3. let _masterName = 'Master';
  4. it('should pass properties to children properly', function () {
  5. let parent = element.all(by.tagName('hero-parent')).get(0);
  6. let heroes = parent.all(by.tagName('hero-child'));
  7. for (let i = 0; i < _heroNames.length; i++) {
  8. let childTitle = heroes.get(i).element(by.tagName('h3')).getText();
  9. let childDetail = heroes.get(i).element(by.tagName('p')).getText();
  10. expect(childTitle).toEqual(_heroNames[i] + ' says:');
  11. expect(childDetail).toContain(_masterName);
  12. }
  13. });
  14. // ...

回到顶部

通过setter截听输入属性值的变化

使用一个输入属性的setter,以拦截父组件中值的变化,并采取行动。

子组件NameChildComponent的输入属性name上的这个setter,会trim掉名字里的空格,并把空值替换成默认字符串。

  1. import { Component, Input } from '@angular/core';
  2. @Component({
  3. selector: 'name-child',
  4. template: '<h3>"{{name}}"</h3>'
  5. })
  6. export class NameChildComponent {
  7. private _name = '';
  8. @Input()
  9. set name(name: string) {
  10. this._name = (name && name.trim()) || '<no name set>';
  11. }
  12. get name(): string { return this._name; }
  13. }

下面的NameParentComponent展示了各种名字的处理方式,包括一个全是空格的名字。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'name-parent',
  4. template: `
  5. <h2>Master controls {{names.length}} names</h2>
  6. <name-child *ngFor="let name of names" [name]="name"></name-child>
  7. `
  8. })
  9. export class NameParentComponent {
  10. // Displays 'Mr. IQ', '<no name set>', 'Bombasto'
  11. names = ['Mr. IQ', ' ', ' Bombasto '];
  12. }
Parent-to-child-setter

测试

端到端测试:输入属性的setter,分别使用空名字和非空名字。

  1. // ...
  2. it('should display trimmed, non-empty names', function () {
  3. let _nonEmptyNameIndex = 0;
  4. let _nonEmptyName = '"Mr. IQ"';
  5. let parent = element.all(by.tagName('name-parent')).get(0);
  6. let hero = parent.all(by.tagName('name-child')).get(_nonEmptyNameIndex);
  7. let displayName = hero.element(by.tagName('h3')).getText();
  8. expect(displayName).toEqual(_nonEmptyName);
  9. });
  10. it('should replace empty name with default name', function () {
  11. let _emptyNameIndex = 1;
  12. let _defaultName = '"<no name set>"';
  13. let parent = element.all(by.tagName('name-parent')).get(0);
  14. let hero = parent.all(by.tagName('name-child')).get(_emptyNameIndex);
  15. let displayName = hero.element(by.tagName('h3')).getText();
  16. expect(displayName).toEqual(_defaultName);
  17. });
  18. // ...

回到顶部

通过ngOnChanges()来截听输入属性值的变化

使用OnChanges生命周期钩子接口的ngOnChanges()方法来监测输入属性值的变化并做出回应。

当需要监视多个、交互式输入属性的时候,本方法比用属性的setter更合适。

学习关于ngOnChanges()的更多知识,参见生命周期钩子一章。

这个VersionChildComponent会监测输入属性majorminor的变化,并把这些变化编写成日志以报告这些变化。

  1. import { Component, Input, OnChanges, SimpleChange } from '@angular/core';
  2. @Component({
  3. selector: 'version-child',
  4. template: `
  5. <h3>Version {{major}}.{{minor}}</h3>
  6. <h4>Change log:</h4>
  7. <ul>
  8. <li *ngFor="let change of changeLog">{{change}}</li>
  9. </ul>
  10. `
  11. })
  12. export class VersionChildComponent implements OnChanges {
  13. @Input() major: number;
  14. @Input() minor: number;
  15. changeLog: string[] = [];
  16. ngOnChanges(changes: {[propKey: string]: SimpleChange}) {
  17. let log: string[] = [];
  18. for (let propName in changes) {
  19. let changedProp = changes[propName];
  20. let to = JSON.stringify(changedProp.currentValue);
  21. if (changedProp.isFirstChange()) {
  22. log.push(`Initial value of ${propName} set to ${to}`);
  23. } else {
  24. let from = JSON.stringify(changedProp.previousValue);
  25. log.push(`${propName} changed from ${from} to ${to}`);
  26. }
  27. }
  28. this.changeLog.push(log.join(', '));
  29. }
  30. }

VersionParentComponent提供minormajor值,把修改它们值的方法绑定到按钮上。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'version-parent',
  4. template: `
  5. <h2>Source code version</h2>
  6. <button (click)="newMinor()">New minor version</button>
  7. <button (click)="newMajor()">New major version</button>
  8. <version-child [major]="major" [minor]="minor"></version-child>
  9. `
  10. })
  11. export class VersionParentComponent {
  12. major: number = 1;
  13. minor: number = 23;
  14. newMinor() {
  15. this.minor++;
  16. }
  17. newMajor() {
  18. this.major++;
  19. this.minor = 0;
  20. }
  21. }

下面是点击按钮的结果。

Parent-to-child-onchanges

测试

测试确保这两个输入属性值都被初始化了,当点击按钮后,ngOnChanges应该被调用,属性的值也符合预期。

  1. // ...
  2. // Test must all execute in this exact order
  3. it('should set expected initial values', function () {
  4. let actual = getActual();
  5. let initialLabel = 'Version 1.23';
  6. let initialLog = 'Initial value of major set to 1, Initial value of minor set to 23';
  7. expect(actual.label).toBe(initialLabel);
  8. expect(actual.count).toBe(1);
  9. expect(actual.logs.get(0).getText()).toBe(initialLog);
  10. });
  11. it('should set expected values after clicking \'Minor\' twice', function () {
  12. let repoTag = element(by.tagName('version-parent'));
  13. let newMinorButton = repoTag.all(by.tagName('button')).get(0);
  14. newMinorButton.click().then(function() {
  15. newMinorButton.click().then(function() {
  16. let actual = getActual();
  17. let labelAfter2Minor = 'Version 1.25';
  18. let logAfter2Minor = 'minor changed from 24 to 25';
  19. expect(actual.label).toBe(labelAfter2Minor);
  20. expect(actual.count).toBe(3);
  21. expect(actual.logs.get(2).getText()).toBe(logAfter2Minor);
  22. });
  23. });
  24. });
  25. it('should set expected values after clicking \'Major\' once', function () {
  26. let repoTag = element(by.tagName('version-parent'));
  27. let newMajorButton = repoTag.all(by.tagName('button')).get(1);
  28. newMajorButton.click().then(function() {
  29. let actual = getActual();
  30. let labelAfterMajor = 'Version 2.0';
  31. let logAfterMajor = 'major changed from 1 to 2, minor changed from 25 to 0';
  32. expect(actual.label).toBe(labelAfterMajor);
  33. expect(actual.count).toBe(4);
  34. expect(actual.logs.get(3).getText()).toBe(logAfterMajor);
  35. });
  36. });
  37. function getActual() {
  38. let versionTag = element(by.tagName('version-child'));
  39. let label = versionTag.element(by.tagName('h3')).getText();
  40. let ul = versionTag.element((by.tagName('ul')));
  41. let logs = ul.all(by.tagName('li'));
  42. return {
  43. label: label,
  44. logs: logs,
  45. count: logs.count()
  46. };
  47. }
  48. // ...

回到顶部

父组件监听子组件的事件

子组件暴露一个EventEmitter属性,当事件发生时,子组件利用该属性emits(向上弹射)事件。父组件绑定到这个事件属性,并在事件发生时作出回应。

子组件的EventEmitter属性是一个输出属性,通常带有@Output装饰器,就像在VoterComponent中看到的。

  1. import { Component, EventEmitter, Input, Output } from '@angular/core';
  2. @Component({
  3. selector: 'my-voter',
  4. template: `
  5. <h4>{{name}}</h4>
  6. <button (click)="vote(true)" [disabled]="voted">Agree</button>
  7. <button (click)="vote(false)" [disabled]="voted">Disagree</button>
  8. `
  9. })
  10. export class VoterComponent {
  11. @Input() name: string;
  12. @Output() onVoted = new EventEmitter<boolean>();
  13. voted = false;
  14. vote(agreed: boolean) {
  15. this.onVoted.emit(agreed);
  16. this.voted = true;
  17. }
  18. }

点击按钮会触发truefalse(布尔型有效载荷)的事件。

父组件VoteTakerComponent绑定了一个事件处理器(onVoted()),用来响应子组件的事件($event)并更新一个计数器。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'vote-taker',
  4. template: `
  5. <h2>Should mankind colonize the Universe?</h2>
  6. <h3>Agree: {{agreed}}, Disagree: {{disagreed}}</h3>
  7. <my-voter *ngFor="let voter of voters"
  8. [name]="voter"
  9. (onVoted)="onVoted($event)">
  10. </my-voter>
  11. `
  12. })
  13. export class VoteTakerComponent {
  14. agreed = 0;
  15. disagreed = 0;
  16. voters = ['Mr. IQ', 'Ms. Universe', 'Bombasto'];
  17. onVoted(agreed: boolean) {
  18. agreed ? this.agreed++ : this.disagreed++;
  19. }
  20. }

框架(Angular)把事件参数(用$event表示)传给事件处理方法,这个方法会处理:

Child-to-parent

测试

测试确保点击AgreeDisagree按钮时,计数器被正确更新。

  1. // ...
  2. it('should not emit the event initially', function () {
  3. let voteLabel = element(by.tagName('vote-taker'))
  4. .element(by.tagName('h3')).getText();
  5. expect(voteLabel).toBe('Agree: 0, Disagree: 0');
  6. });
  7. it('should process Agree vote', function () {
  8. let agreeButton1 = element.all(by.tagName('my-voter')).get(0)
  9. .all(by.tagName('button')).get(0);
  10. agreeButton1.click().then(function() {
  11. let voteLabel = element(by.tagName('vote-taker'))
  12. .element(by.tagName('h3')).getText();
  13. expect(voteLabel).toBe('Agree: 1, Disagree: 0');
  14. });
  15. });
  16. it('should process Disagree vote', function () {
  17. let agreeButton1 = element.all(by.tagName('my-voter')).get(1)
  18. .all(by.tagName('button')).get(1);
  19. agreeButton1.click().then(function() {
  20. let voteLabel = element(by.tagName('vote-taker'))
  21. .element(by.tagName('h3')).getText();
  22. expect(voteLabel).toBe('Agree: 1, Disagree: 1');
  23. });
  24. });
  25. // ...

回到顶部

父组件与子组件通过本地变量互动

父组件不能使用数据绑定来读取子组件的属性或调用子组件的方法。但可以在父组件模板里,新建一个本地变量来代表子组件,然后利用这个变量来读取子组件的属性和调用子组件的方法,如下例所示。

子组件CountdownTimerComponent进行倒计时,归零时发射一个导弹。startstop方法负责控制时钟并在模板里显示倒计时的状态信息。

  1. import { Component, OnDestroy, OnInit } from '@angular/core';
  2. @Component({
  3. selector: 'countdown-timer',
  4. template: '<p>{{message}}</p>'
  5. })
  6. export class CountdownTimerComponent implements OnInit, OnDestroy {
  7. intervalId = 0;
  8. message = '';
  9. seconds = 11;
  10. clearTimer() { clearInterval(this.intervalId); }
  11. ngOnInit() { this.start(); }
  12. ngOnDestroy() { this.clearTimer(); }
  13. start() { this.countDown(); }
  14. stop() {
  15. this.clearTimer();
  16. this.message = `Holding at T-${this.seconds} seconds`;
  17. }
  18. private countDown() {
  19. this.clearTimer();
  20. this.intervalId = window.setInterval(() => {
  21. this.seconds -= 1;
  22. if (this.seconds === 0) {
  23. this.message = 'Blast off!';
  24. } else {
  25. if (this.seconds < 0) { this.seconds = 10; } // reset
  26. this.message = `T-${this.seconds} seconds and counting`;
  27. }
  28. }, 1000);
  29. }
  30. }

让我们来看看计时器组件的宿主组件CountdownLocalVarParentComponent

  1. import { Component } from '@angular/core';
  2. import { CountdownTimerComponent } from './countdown-timer.component';
  3. @Component({
  4. selector: 'countdown-parent-lv',
  5. template: `
  6. <h3>Countdown to Liftoff (via local variable)</h3>
  7. <button (click)="timer.start()">Start</button>
  8. <button (click)="timer.stop()">Stop</button>
  9. <div class="seconds">{{timer.seconds}}</div>
  10. <countdown-timer #timer></countdown-timer>
  11. `,
  12. styleUrls: ['demo.css']
  13. })
  14. export class CountdownLocalVarParentComponent { }

父组件不能通过数据绑定使用子组件的startstop方法,也不能访问子组件的seconds属性。

把本地变量(#timer)放到(<countdown-timer>)标签中,用来代表子组件。这样父组件的模板就得到了子组件的引用,于是可以在父组件的模板中访问子组件的所有属性和方法。

在这个例子中,我们把父组件的按钮绑定到子组件的startstop方法,并用插值表达式来显示子组件的seconds属性。

下面是父组件和子组件一起工作时的效果。

countdown timer

测试

测试确保在父组件模板中显示的秒数和子组件状态信息里的秒数同步。它还会点击Stop按钮来停止倒计时:

  1. // ...
  2. it('timer and parent seconds should match', function () {
  3. let parent = element(by.tagName(parentTag));
  4. let message = parent.element(by.tagName('countdown-timer')).getText();
  5. browser.sleep(10); // give `seconds` a chance to catchup with `message`
  6. let seconds = parent.element(by.className('seconds')).getText();
  7. expect(message).toContain(seconds);
  8. });
  9. it('should stop the countdown', function () {
  10. let parent = element(by.tagName(parentTag));
  11. let stopButton = parent.all(by.tagName('button')).get(1);
  12. stopButton.click().then(function() {
  13. let message = parent.element(by.tagName('countdown-timer')).getText();
  14. expect(message).toContain('Holding');
  15. });
  16. });
  17. // ...

回到顶部

父组件调用@ViewChild()

这个本地变量方法是个简单便利的方法。但是它也有局限性,因为父组件-子组件的连接必须全部在父组件的模板中进行。父组件本身的代码对子组件没有访问权。

如果父组件的需要读取子组件的属性值或调用子组件的方法,就不能使用本地变量方法。

当父组件需要这种访问时,可以把子组件作为ViewChild注入到父组件里面。

下面的例子用与倒计时相同的范例来解释这种技术。 我们没有改变它的外观或行为。子组件CountdownTimerComponent也和原来一样。

本地变量切换到ViewChild技术的唯一目的就是做示范。

下面是父组件CountdownViewChildParentComponent:

  1. import { AfterViewInit, ViewChild } from '@angular/core';
  2. import { Component } from '@angular/core';
  3. import { CountdownTimerComponent } from './countdown-timer.component';
  4. @Component({
  5. selector: 'countdown-parent-vc',
  6. template: `
  7. <h3>Countdown to Liftoff (via ViewChild)</h3>
  8. <button (click)="start()">Start</button>
  9. <button (click)="stop()">Stop</button>
  10. <div class="seconds">{{ seconds() }}</div>
  11. <countdown-timer></countdown-timer>
  12. `,
  13. styleUrls: ['demo.css']
  14. })
  15. export class CountdownViewChildParentComponent implements AfterViewInit {
  16. @ViewChild(CountdownTimerComponent)
  17. private timerComponent: CountdownTimerComponent;
  18. seconds() { return 0; }
  19. ngAfterViewInit() {
  20. // Redefine `seconds()` to get from the `CountdownTimerComponent.seconds` ...
  21. // but wait a tick first to avoid one-time devMode
  22. // unidirectional-data-flow-violation error
  23. setTimeout(() => this.seconds = () => this.timerComponent.seconds, 0);
  24. }
  25. start() { this.timerComponent.start(); }
  26. stop() { this.timerComponent.stop(); }
  27. }

把子组件的视图插入到父组件类需要做一点额外的工作。

首先,你要使用ViewChild装饰器导入这个引用,并挂上AfterViewInit生命周期钩子。

接着,通过@ViewChild属性装饰器,将子组件CountdownTimerComponent注入到私有属性timerComponent里面。

组件元数据里就不再需要#timer本地变量了。而是把按钮绑定到父组件自己的startstop方法,使用父组件的seconds方法的插值表达式来展示秒数变化。

这些方法可以直接访问被注入的计时器组件。

ngAfterViewInit()生命周期钩子是非常重要的一步。被注入的计时器组件只有在Angular显示了父组件视图之后才能访问,所以我们先把秒数显示为0.

然后Angular会调用ngAfterViewInit生命周期钩子,但这时候再更新父组件视图的倒计时就已经太晚了。Angular的单向数据流规则会阻止在同一个周期内更新父组件视图。我们在显示秒数之前会被迫再等一轮

使用setTimeout()来等下一轮,然后改写seconds()方法,这样它接下来就会从注入的这个计时器组件里获取秒数的值。

测试

使用和之前一样的倒计时测试

回到顶部

父组件和子组件通过服务来通讯

父组件和它的子组件共享同一个服务,利用该服务在家庭内部实现双向通讯。

该服务实例的作用域被限制在父组件和其子组件内。这个组件子树之外的组件将无法访问该服务或者与它们通讯。

这个MissionServiceMissionControlComponent和多个AstronautComponent子组件连接起来。

  1. import { Injectable } from '@angular/core';
  2. import { Subject } from 'rxjs/Subject';
  3. @Injectable()
  4. export class MissionService {
  5. // Observable string sources
  6. private missionAnnouncedSource = new Subject<string>();
  7. private missionConfirmedSource = new Subject<string>();
  8. // Observable string streams
  9. missionAnnounced$ = this.missionAnnouncedSource.asObservable();
  10. missionConfirmed$ = this.missionConfirmedSource.asObservable();
  11. // Service message commands
  12. announceMission(mission: string) {
  13. this.missionAnnouncedSource.next(mission);
  14. }
  15. confirmMission(astronaut: string) {
  16. this.missionConfirmedSource.next(astronaut);
  17. }
  18. }

MissionControlComponent提供服务的实例,并将其共享给它的子组件(通过providers元数据数组),子组件可以通过构造函数将该实例注入到自身。

  1. import { Component } from '@angular/core';
  2. import { MissionService } from './mission.service';
  3. @Component({
  4. selector: 'mission-control',
  5. template: `
  6. <h2>Mission Control</h2>
  7. <button (click)="announce()">Announce mission</button>
  8. <my-astronaut *ngFor="let astronaut of astronauts"
  9. [astronaut]="astronaut">
  10. </my-astronaut>
  11. <h3>History</h3>
  12. <ul>
  13. <li *ngFor="let event of history">{{event}}</li>
  14. </ul>
  15. `,
  16. providers: [MissionService]
  17. })
  18. export class MissionControlComponent {
  19. astronauts = ['Lovell', 'Swigert', 'Haise'];
  20. history: string[] = [];
  21. missions = ['Fly to the moon!',
  22. 'Fly to mars!',
  23. 'Fly to Vegas!'];
  24. nextMission = 0;
  25. constructor(private missionService: MissionService) {
  26. missionService.missionConfirmed$.subscribe(
  27. astronaut => {
  28. this.history.push(`${astronaut} confirmed the mission`);
  29. });
  30. }
  31. announce() {
  32. let mission = this.missions[this.nextMission++];
  33. this.missionService.announceMission(mission);
  34. this.history.push(`Mission "${mission}" announced`);
  35. if (this.nextMission >= this.missions.length) { this.nextMission = 0; }
  36. }
  37. }

AstronautComponent也通过自己的构造函数注入该服务。由于每个AstronautComponent都是MissionControlComponent的子组件,所以它们获取到的也是父组件的这个服务实例。

  1. import { Component, Input, OnDestroy } from '@angular/core';
  2. import { MissionService } from './mission.service';
  3. import { Subscription } from 'rxjs/Subscription';
  4. @Component({
  5. selector: 'my-astronaut',
  6. template: `
  7. <p>
  8. {{astronaut}}: <strong>{{mission}}</strong>
  9. <button
  10. (click)="confirm()"
  11. [disabled]="!announced || confirmed">
  12. Confirm
  13. </button>
  14. </p>
  15. `
  16. })
  17. export class AstronautComponent implements OnDestroy {
  18. @Input() astronaut: string;
  19. mission = '<no mission announced>';
  20. confirmed = false;
  21. announced = false;
  22. subscription: Subscription;
  23. constructor(private missionService: MissionService) {
  24. this.subscription = missionService.missionAnnounced$.subscribe(
  25. mission => {
  26. this.mission = mission;
  27. this.announced = true;
  28. this.confirmed = false;
  29. });
  30. }
  31. confirm() {
  32. this.confirmed = true;
  33. this.missionService.confirmMission(this.astronaut);
  34. }
  35. ngOnDestroy() {
  36. // prevent memory leak when component destroyed
  37. this.subscription.unsubscribe();
  38. }
  39. }

注意,这个例子保存了subscription变量,并在AstronautComponent被销毁时调用unsubscribe()退订。 这是一个用于防止内存泄漏的保护措施。实际上,在这个应用程序中并没有这个风险,因为AstronautComponent的生命期和应用程序的生命期一样长。但在更复杂的应用程序环境中就不一定了。

不需要在MissionControlComponent中添加这个保护措施,因为它作为父组件,控制着MissionService的生命期。

History日志证明了:在父组件MissionControlComponent和子组件AstronautComponent之间,信息通过该服务实现了双向传递。

bidirectional-service

测试

测试确保点击父组件MissionControlComponent和子组件AstronautComponent两个的组件的按钮时,History日志和预期的一样。

  1. // ...
  2. it('should announce a mission', function () {
  3. let missionControl = element(by.tagName('mission-control'));
  4. let announceButton = missionControl.all(by.tagName('button')).get(0);
  5. announceButton.click().then(function () {
  6. let history = missionControl.all(by.tagName('li'));
  7. expect(history.count()).toBe(1);
  8. expect(history.get(0).getText()).toMatch(/Mission.* announced/);
  9. });
  10. });
  11. it('should confirm the mission by Lovell', function () {
  12. testConfirmMission(1, 2, 'Lovell');
  13. });
  14. it('should confirm the mission by Haise', function () {
  15. testConfirmMission(3, 3, 'Haise');
  16. });
  17. it('should confirm the mission by Swigert', function () {
  18. testConfirmMission(2, 4, 'Swigert');
  19. });
  20. function testConfirmMission(buttonIndex: number, expectedLogCount: number, astronaut: string) {
  21. let _confirmedLog = ' confirmed the mission';
  22. let missionControl = element(by.tagName('mission-control'));
  23. let confirmButton = missionControl.all(by.tagName('button')).get(buttonIndex);
  24. confirmButton.click().then(function () {
  25. let history = missionControl.all(by.tagName('li'));
  26. expect(history.count()).toBe(expectedLogCount);
  27. expect(history.get(expectedLogCount - 1).getText()).toBe(astronaut + _confirmedLog);
  28. });
  29. }
  30. // ...

回到顶部