Ionics 3 Guard 问题

Ionics 3 Guard issue

我意识到在 ionViewCanEnter 决定之前加载页面,我什至允许用户查看页面。

因此,模板中的所有组件都会得到构建。

IMO,这可能会变得非常低效,尤其是当组件在其构造函数中使用 http 请求来加载数据时。

我是不是弄错了什么或者是否有更好的 Guard 方法?

简单副本

测试-page.html

<ion-content>
  <test></test>
</ion-content>

测试-page.ts

...
ionViewCanEnter() {
  console.log('ionViewCanEnter?');
  return false;
}
...

测试-component.ts

...
constructor() {
  console.log('TestComponent Constructed');
}
...

控制台

TestComponent Constructed
ionViewCanEnter?

您期待的东西与 IonicPage/Component 的设计不同。

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

你可以看到它说 runs before the view can enter.Not before the view has been created.If Page's view has been created 意味着 [= page 中的 14=] 也是 constructed。因此 design.We 对此无能为力。但是您可以更改 component 的数据检索方式。换句话说,你需要根据 ionViewCanEnter() 的逻辑发出一个 Event 然后,你可以 subscribe 到你的 component 里面的那个 event对于数据 retrievals.After ,您可以删除组件 constructor 内的数据检索。希望之后一切顺利。