更改路线之前,Angular Call webapi

时间:2019-05-31 17:24:51

标签: angular angular-router

---想要在Angular中更改路线之前调用webapi ---

我有两个页面,其中包含两个部分:

页面: 首页与关于

组件: home.component.ts&about.component.ts

当我从 HOME重定向到ABOUT 页面时,反之亦然,我想先调用Webapi,然后再路由到另一页面

我尝试了下面提到的两件事:

  1. ngOnDestroy
  2. 订阅路由器事件

示例:这是我到目前为止尝试过的

假设我要从Home导航=> About页面,因此在home component方法内部,以下是两种情况

Home.component.ts ::::调用ngOnDestroy方法内的Webapi方法。

------------------------------------------ ngOnDestroy ---- -------------------------------------------------- ---

public ngOnDestroy():void {

    this.updateChanges(); // Indside this method, I have a service class that handles all http requests call

    // Unsubscribe from all subscriptions
    this._unsubscribeAll.next();
    this._unsubscribeAll.complete();

}

--------------------------------------------------路由器------------------------------------------------- -----------

Home.component.ts :::::通过订阅路由器更改事件来调用Webapi

public ngOnInit():void {

this._router.events.subscribe((ev: Event) => {
           if (ev instanceof NavigationStart) {
                this.updateChanges(); // Indside this method, I have a service class that handles all http requests call
           }
        }
);

}


问题:在浏览器的“网络”选项卡中,我可以看到正在调用Webapi,但是状态显示为“已取消”。知道为什么吗?

仅供参考:Webapi仅正确。页面运行正常。

0 个答案:

没有答案
相关问题