簡體   English   中英

在我的父控制器解析數據之前防止子組件呈現

[英]prevent child component render before my parent controller resolve the data

我的情況

我有一個app.module ,其中引導程序app.component

和路由器基本href ='/'

加載我的home.component

我有方法onInit(),其中我使用RiotService從api獲取數據

this.riot.Summoner.byName(this.summoner, this.region);

所以我可以喜歡

this.riot.Summoner.byName(this.summoner, this.region).then(()=> {
   //do something with data
   //this.riot.Summoner.info
});

下一步

我有我的summoner.component,它有自己的路由“ / summonerName / etc” ,在這里我需要使用從父組件(home.component)檢索到的數據

可以,例如,我先使用[routerLink]進入“ /” ,然后進入“ / [routerLink] / etc”

但是當我直接轉到'/ summonerName / etc'或刷新頁面時

我home.component開始從我的檢索數據RiotServive ,當我試圖讓this.riot.Summoner.info我得到undefined時候,我summoner.component得到了呈現。

我之所以知道全部,是因為我承諾比子組件渲染的解析時間更長。

所以我有一個問題。

我如何在父控制器解析數據之前阻止子組件渲染

這是我的RiotService

request<T>(url:string, errorHandler?: (error: any) => Promise<any>){
    return this.http.get(url)
        .toPromise()
        .then(response => response.json() as T)
        .catch(errorHandler || this.handleError);
}
byName(summonerName:string, region:string){
    return this.request<ISummoner>(
            `some url to get data`)
        .then((response) => {
            this.info = response as ISummoner
        })
}

只需使用Resolvers ,它可以確保在實際激活路由之前加載數據。

下面的文章通過逐步的示例解釋了這種想法

https://blog.thoughtram.io/angular/2016/10/10/resolving-route-data-in-angular-2.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM