【问题标题】:Observable.subscribe() not working in IE on component change in Angular 7Observable.subscribe() 在 IE 中无法在 Angular 7 中更改组件
【发布时间】:2020-03-17 21:25:35
【问题描述】:

我有两个 HTML 按钮“上一个”和“下一个”。我使用这些按钮导航到不同的选项卡(组件)。该代码在 Chrome 和 Firefox 中按预期工作,但在 IE 或 Edge 中单击下一个和上一个按钮时会出现空白屏幕。

“下一个”按钮事件处理程序:

saveAndNextClick(form: FormGroup, section: string) {
  this.markFormGroupTouched(form);
  if (form) {
    if (section === 'lastsection') {
      this.sectionChangeService.change('schoolSection');
    } else {
      this.hideShowTabSection();
      this.selectedSectionGroup[section] = false;
    }
  }
  this.saveData(); 
  this.saveService.saveQuestion();
}

模板

<div class="d-flex app-question-navigation justify-content-between">
  <a class="btn btn-secondary buttonSize (click)="previousClick(appSectionThree,'sectionTwo')">
    Previous
  </a>
  <span class="app-question-pagination">Section 3 of 8</span>
  <a class="btn btn-secondary buttonSize" (click)="saveAndNextClick(appSectionThree,'sectionFour')">
    Next
  </a>
</div>
</form>

组件.ts

ngOnInit() {
  this.sectionChangeService.listen().subscribe((message: any) => {
    if (message.text === 'prvsparentdemosection') {
      this.saveAndNextClick(this.pdemographicsSectionFive, 'sectionFour');
    }
  });
}

Service:

```typescript
import { Injectable } from '@angular/core';
import { Observable, Subject } from 'rxjs';

@Injectable({ providedIn: 'root' })

export class SectionChangeService {
  private listner = new Subject<any>();

  change(message: string) {
    this.listner.next({ text: message });
  }

  clearListner() {
    this.listner.next();
  }

  listen(): Observable<any> {
    return this.listner.asObservable();
  }
}

【问题讨论】:

  • 您是否在浏览器控制台中看到任何错误或警告?如果是,请让我们知道该错误。它可以帮助缩小问题范围。
  • @Deepak-MSFT 我在控制台中看不到任何内容,没有错误
  • 您的项目是在 IE 11 中运行还是显示空白页?我可以在代码中看到箭头函数。那么你是否添加了必要的 polyfill 或使用 Babel 来转译代码?

标签: javascript angular internet-explorer rxjs rxjs-observables


【解决方案1】:

尝试使用 polyfill,IE 和 Edge 需要它们,因为它们缺少一些 es6/7 功能

在 polyfills.ts 文件中导入这个

import 'core-js/es7/array';
import 'core-js/es7/object';
import 'core-js/es6/reflect';
import 'core-js/es7/reflect';`

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    • 1970-01-01
    • 1970-01-01
    • 2016-05-25
    • 2017-12-03
    • 1970-01-01
    相关资源
    最近更新 更多