問題描述
在渲染 Page
之前獲取數據異步的正確方法是什么?
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
裝飾器 見但它沒有記錄,我無法弄清楚它到底做了什么.
Apparently Ionic2
does something with the @CanActivate
decorator, see
But its not documented and i can't figure out what it does exactly.
盡管如此 這家伙指出應該使用 無論如何,由于離子緩存,離子視圖狀態
.他的例子是這樣的:
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);
}
看起來他希望 Ionic 考慮返回的承諾,但是 快速瀏覽 離子源顯示(至少我是這么認為的)返回值被忽略.因此,不能保證在頁面呈現之前 會解決承諾.這是一個帶有 onPage* 的 示例,以及它沒有按需要/預期執行的原因.
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.
所以我迷路了,如何完成這個簡單的任務?
So I'm lost, how does one achieve this simple task?
在第一個鏈接中,建議在導航到頁面之前解析數據,這會增加被調用者知道頁面需要哪些數據的負擔.在我看來,這不是一個選擇.
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.
*添加反例
推薦答案
對于在使用 Ionic 2 時對 Stackoverflow 進行限制頁面訪問的任何人來說,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
.
來自文檔:
ionViewCanEnter 在視圖進入之前運行.這可以用作經過身份驗證的視圖中的一種守衛",您需要在視圖進入之前檢查權限.
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/
這篇關于在渲染“頁面"之前獲取數據異步的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!