問(wèn)題描述
在渲染 Page
之前獲取數(shù)據(jù)異步的正確方法是什么?
What is the correct way to get data async before a Page
gets rendered?
Angular2
建議使用 @CanActivate
裝飾器.可悲的是,這不適用于 Ionic2,至少不適用于我和 其他
Angular2
suggests the @CanActivate
decorator as far as I understand it. Sadly this is not working with Ionic2, at least not for me and for others
顯然 Ionic2
使用 @CanActivate
裝飾器 見(jiàn)但它沒(méi)有記錄,我無(wú)法弄清楚它到底做了什么.
Apparently Ionic2
does something with the @CanActivate
decorator, see
But its not documented and i can't figure out what it does exactly.
盡管如此 這家伙指出應(yīng)該使用 無(wú)論如何,由于離子緩存,離子視圖狀態(tài)
.他的例子是這樣的:
Nevertheless this guy points out one should use Ionics View States
instead anyways, due to ionics caching. His example looks like this:
onPageWillEnter() {
return this._service.getComments().then(data => this.comments = data);
}
看起來(lái)他希望 Ionic 考慮返回的承諾,但是 快速瀏覽 離子源顯示(至少我是這么認(rèn)為的)返回值被忽略.因此,不能保證在頁(yè)面呈現(xiàn)之前 會(huì)解決承諾.這是一個(gè)帶有 onPage* 的 示例,以及它沒(méi)有按需要/預(yù)期執(zhí)行的原因.
Which looks like he is expecting Ionic to consider the returned promise, but a quick glance a Ionics sources reveals (at least I think so) that the returned value is ignored. Hence there is no guarantee that the promise gets resolved before the page gets rendered. Here is an example with onPage* and how it does not perform as needed/expected.
所以我迷路了,如何完成這個(gè)簡(jiǎn)單的任務(wù)?
So I'm lost, how does one achieve this simple task?
在第一個(gè)鏈接中,建議在導(dǎo)航到頁(yè)面之前解析數(shù)據(jù),這會(huì)增加被調(diào)用者知道頁(yè)面需要哪些數(shù)據(jù)的負(fù)擔(dān).在我看來(lái),這不是一個(gè)選擇.
In the first link, it was suggested to resolve the data before navigating to the page, which burdens the knowledge which data is needed for the page on the callee. This is not an option in my opinion.
*添加反例
推薦答案
對(duì)于在使用 Ionic 2 時(shí)對(duì) Stackoverflow 進(jìn)行限制頁(yè)面訪問(wèn)的任何人來(lái)說(shuō),Ionic 推薦的生命周期事件似乎是 ionViewCanEnter
.
For anyone crawling Stackoverflow about restricting page access when using Ionic 2, it looks like Ionic's recommended lifecycle event to tap into is ionViewCanEnter
.
來(lái)自文檔:
ionViewCanEnter 在視圖進(jìn)入之前運(yùn)行.這可以用作經(jīng)過(guò)身份驗(yàn)證的視圖中的一種守衛(wèi)",您需要在視圖進(jìn)入之前檢查權(quán)限.
ionViewCanEnter Runs before the view can enter. This can be used as a sort of "guard" in authenticated views where you need to check permissions before the view can enter.
http://ionicframework.com/docs/v2/api/navigation/NavController/
這篇關(guān)于在渲染“頁(yè)面"之前獲取數(shù)據(jù)異步的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!