Ionics 3 Guard问题

时间:2017-11-06 16:31:31

标签: angular typescript ionic-framework ionic3 guard

我已经意识到在ionViewCanEnter决定之前页面已被加载,我甚至允许用户查看该页面。

因此,模板中的所有组件始终都是构建的。

IMO,这可能变得非常低效,特别是如果组件在其构造函数中使用http请求来加载数据。

我出错了还是有更好的警卫方法?

简单副本

测试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?

1 个答案:

答案 0 :(得分:1)

您期待与Page/ComponentIonic的设计相悖的不同内容。

ionViewCanEnter

  

在视图可以进入之前运行。这可以用作一种&#34;警卫&#34;   在经过身份验证的视图中,您需要在之前检查权限   视图可以输入

在创建视图之前,您可以看到它显示runs before the view can enter。如果已创建网页view,则表示components其中page也是constructed。所以这是设计的。我们不能为此做任何事情。但您可以更改component数据检索的方式。换句话说,您需要根据ionViewCanEnter()的逻辑发出Event,之后,subscribe event内的component constructor用于数据检索。之后,您可以删除组件 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if let cell:UITableViewCell = countryList.dequeueReusableCell(withIdentifier: "cell")! as UITableViewCell // Error 1 happens here { let text: String! if inSearchMode { text = filteredCountriesList[indexPath.row] } else { text = countriesList[indexPath.row] } cell.congigureCell(text: text) // Error 2 happens here return cell } else { return UITableViewCell() } } 内的数据检索。希望在此之后一切正常。