【问题标题】:Angular2 OnInit Not Firing IE11Angular2 OnInit 不触发 IE11
【发布时间】:2016-11-21 06:09:51
【问题描述】:

当您导航到另一个 angular2 路线时,IE11 似乎没有触发 OnInit,然后返回到原始路线。所以要明确 route1 是有问题的路线,是默认路线。当 route1 加载 OnInit 在我的 component1 中很好地触发时,我导航到 route2/component2 OnInit 也很好。问题是当我导航回 route1/component1 OnInit 在 IE 中不会触发,但在所有其他浏览器中都会触发。我们在 RC3 上。

这是我的组件:

import { Component, OnInit} from '@angular/core';
import { UdrService} from './udr.service';
import { Widget} from '../widget';

@Component({
    selector: 'udr',
    templateUrl: './Home/RenderUDR',
    providers: [UdrService]
})

export class UdrComponent implements OnInit {
    constructor(private _udrService: UdrService) { }

    widgets: Widget[];

    ngOnInit() {
        this._udrService.getUdrActiveWidgets()
            .subscribe(wdgts => this.widgets = wdgts);
    }
}

谢谢!

编辑 1 --- 我注意到当 DOM 中发生其他事件时(即在元素上触发 onhover,或者我打开一个下拉菜单,OnInit 方法触发,似乎当 angular2 更改检测触发时它最终触发 OnInit)

编辑 2 --- 该问题仍然存在于 rc4 中。看来我的问题是这样的:

https://github.com/angular/angular/issues/10146

链接到修复显示 router.ts 的更新。我没有看到该文件捆绑在 npm 包中,只有转译的 router.js 文件。如何应用此更新?

谢谢

【问题讨论】:

标签: angular ng-init


【解决方案1】:

在 Angular2 github 网站上似乎有几个关于这个问题的报告是重复的。我从这个链接中解除了一项工作: GitHub Issue

我必须做的唯一修改是 _router.events.subscribe 而不是 _router.subscribe 因为我认为 _router.events.subscribe 对组件路由器来说是新的。

export class AppComponent {

constructor(private _applicationRef: ApplicationRef, private _router: Router) {

    _router.events.subscribe(() => {
        this._applicationRef.tick();
        setTimeout(() => {
            this._applicationRef.tick();
        }, 100);
    });

}

我希望这在 RC5 中得到解决,但这似乎目前可行。

【讨论】:

  • 您使用的是已弃用的路由器吗?我的 _router 对象没有 .events 或 .subscribe()。
  • "@angular/router": "3.0.0-beta.1",
猜你喜欢
  • 1970-01-01
  • 2016-09-12
  • 1970-01-01
  • 2016-12-13
  • 2017-05-10
  • 1970-01-01
  • 2018-11-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多