Ionic 3 Component vs Page [英] Ionic 3 Component vs Page

查看:93
本文介绍了Ionic 3 Component vs Page的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您能否告诉我 Component Page 生成器之间有什么区别? Ionic 3 app?看来我也可以在组件内部使用页面生命周期钩子,如 ionViewWillLeave 。那么什么时候应该使用角度生命周期钩子呢?如果它是相同的那么为什么它有2个发电机?希望你能为此提供反馈。

Can you tell me what is the difference between Component and Page generator in the Ionic 3 app? It seems I can use page life cycle hooks like ionViewWillLeave inside the component too.So when should I use angular life cycle hooks then? If it is same then Why it has 2 generators? Hope you'll provide a feedback for this.

组件生成器:

 ionic generate component SubscribeTopicComponent

页面生成器:

ionic generate page LoginPage


推荐答案

根据评论中的对话:

从Angular的角度来看,它可能是相同的,但页面和组件在离子中具有不同的含义。 就Angular而言,两者都只是组件,但在Ionic的上下文中是,Page是一个充当整个视图的组件 (它可能有嵌套组件);我们将Ionic页面视为独立概念。在Angular应用程序中,组件大部分时间只是更大组件的一部分,所以我猜这是与Pages的最大区别。

It may be the same from the Angular point of view, but Pages and Components have a different meaning in Ionic. In terms of Angular, both are just components, but in the context of Ionic, a Page is a component that will act as an entire view (it may have nested components); we see Ionic pages as a standalone concept. A component will be just part of a bigger component most of the time in Angular apps, so I guess that's the biggest difference with Pages.

关于何时使用Angular的生命周期钩子,我喜欢在嵌套组件中使用它们,但在页面上工作时我更喜欢Ionic生命周期钩子。主要是因为像 ionViewWillEnter 之类的东西在一个简单组件的上下文中没有太大意义,其中 ngOnInit 可以。话虽这么说,我还在Pages上使用了一些Angular生命周期钩子,比如 ngOnDestroy (我用它来删除页面中的所有订阅,当该页面将被销毁时),但就像你说的那样,如果我们想使用Ionic的生命周期钩子, ionViewWillUnload 似乎是正确的方法。

About when using Angular's lifecycle hooks, I like to use them when working in nested components, but I prefer Ionic lifecycle hooks when working on pages. Mostly because things like ionViewWillEnter doesn't make too much sense in the context of a simple component, where ngOnInit does. That being said, I also used some Angular lifecycle hooks on Pages, like the ngOnDestroy (I used it to remove all the subscriptions from a page when that page is going to be destroyed), but just like you said, ionViewWillUnload seems to be the right way to do it if we want to use Ionic's lifecycle hooks.

我想Ionic生命周期挂钩的大多数与用户与整个页面交互的方式更相关(将进入页面,将离开页面,可以进入页面,可以离开页面...)和Angular生命周期钩子与单个组件的生命的不同阶段更相关(输入具有已初始化,变更检测器已检查此组件是否有变化,...),如您所见,可能与用户交互根本没有直接关系,通常是用户不知道的事情。

I guess that most of the Ionic lifecycle hooks are more related to the way the user interacts with the page as a whole (will enter to a page, will leave from a page, can enter to a page, can leave from a page...) and Angular lifecycle hooks are more related to the different stages of the life of a single component (the inputs has been initialized, the change detector has checked if there were changes in this component, ...), which as you can see, may not be directly related to the user interaction at all, and usually are things that the user is not aware of.

我很确定e不是关于哪种方法更好的规则,但最重要的是一致性。 我认为在组件中使用Ionic生命周期钩子是有意义的,并在嵌套组件中使用Angular生命周期钩子,但是你可以使用不同的方法,只要你在整个应用程序。

I'm pretty sure there isn't a rule about which approach is better, but the most important thing is consistency. I think it make sense to use Ionic lifecycle hooks in the components that are Pages, and use Angular lifecycle hooks inside of nested components, but you can use a different approach, as long as you do it consistently in the entire app.

这篇关于Ionic 3 Component vs Page的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆