什么先运行 - 构造函数或 ionView ......?
What runs first - constructor or ionView...?
在 Ionic 框架中,运行首先是构造函数还是 ionViewDidEnter?这个有保障吗?
构造函数与其他每个事件页面生命周期事件相比怎么样?是否可以保证构造函数或任何离子页面生命周期事件将首先 运行?
constructor() 方法不是 Ionic 的特性,它是 ES6 class(大多数情况下是 Typescript)的特性。所以 Ionic 框架不调用构造函数方法,javascript 引擎负责调用构造函数。所以,
这是页面启动时触发的第一个生命周期事件。
现在回答你的问题
在Ionic框架中,运行首先是constructor还是ionViewDidEnter?这个有保障吗?
如果页面没有加载到内存中,那么总是先调用构造函数。其次是Ionic提供的其他生命周期事件(查看下面粘贴的图片以查看事件顺序)。
如果页面已经加载到内存中,则不会调用构造函数。
在这种情况下,ionViewCanEnter 将是第一个被触发的事件,然后 ionViewWillEnter 甚至将被触发,然后 ionViewDidEnter 事件将被触发。
注意: ionViewDidLoad 仅在页面加载到内存时触发一次(类似于构造函数)
下一题,
构造函数与其他每个事件页面生命周期事件相比怎么样?是否可以保证构造函数或任何离子页面生命周期事件将首先 运行?
正如我之前提到的,如果页面尚未加载,构造函数将始终首先被调用。如果已经加载,则不会调用构造函数。
下图显示了 Ionic Page 生命周期事件的顺序。
每个事件的一点解释。
ionViewCanEnter
This event is an Ion Nav Guard Fired before entering into a view(page), allows you to control whether the view can
be accessed or not (returning true or false). If the page is already loaded into the memory, this will be the first event to be fired. If not, This method will be fired after the constructor.
ionViewDidLoad
Fired only when a view is stored in memory. This event is NOT fired on
entering a view that is already cached. The difference between the constructor and IonViewDidLoad is that the constructor should be as thin as possible, meaning you should only be initializing the variables of your class. This is because making calls to REST API and initializing data in the constructor would cause the application to slow down as the creation of the object takes time. Whereas in ionViewDidLoad event you can be sure that all your variables & dependencies injected are available for use and it's also a good place to make REST API calls and initialize the data.
ionViewWillEnter
This event is fired when entering a page before it becomes the active
one. You can use this event for tasks that has to be done every time
when a view is entered (ex: Updating a table).
ionViewDidEnter
Fired when entering a page, after it becomes the active page.
ionViewCanLeave
This event is also nav guard like ionViewCanEnter. This event is fired
before leaving a view, allows you to control whether the view can be
left or not.
ionViewWillLeave
This event is fired when you leave a page. At this stage, your page is
still in an active state but it has been queued up to be removed and
you can no longer prevent the page from being transitioned away.
ionViewDidLeave
At this state, the page has become inactive. It is important to note
that this event will be fired after ionViewDidEnter of the next page
has fired. You can use this method to save data or state that can be
used when this page is visited again ( Only if the page is not
destroyed).
ionViewDidUnload
Fired when a view is going to be completely removed (after leaving a
non-cached view). This method can be used to free up the resources
that is no longer required
参考:https://blog.ionicframework.com/navigating-lifecycle-events/
在 Ionic 框架中,运行首先是构造函数还是 ionViewDidEnter?这个有保障吗?
构造函数与其他每个事件页面生命周期事件相比怎么样?是否可以保证构造函数或任何离子页面生命周期事件将首先 运行?
constructor() 方法不是 Ionic 的特性,它是 ES6 class(大多数情况下是 Typescript)的特性。所以 Ionic 框架不调用构造函数方法,javascript 引擎负责调用构造函数。所以, 这是页面启动时触发的第一个生命周期事件。
现在回答你的问题
在Ionic框架中,运行首先是constructor还是ionViewDidEnter?这个有保障吗?
如果页面没有加载到内存中,那么总是先调用构造函数。其次是Ionic提供的其他生命周期事件(查看下面粘贴的图片以查看事件顺序)。
如果页面已经加载到内存中,则不会调用构造函数。 在这种情况下,ionViewCanEnter 将是第一个被触发的事件,然后 ionViewWillEnter 甚至将被触发,然后 ionViewDidEnter 事件将被触发。
注意: ionViewDidLoad 仅在页面加载到内存时触发一次(类似于构造函数)
下一题,
构造函数与其他每个事件页面生命周期事件相比怎么样?是否可以保证构造函数或任何离子页面生命周期事件将首先 运行?
正如我之前提到的,如果页面尚未加载,构造函数将始终首先被调用。如果已经加载,则不会调用构造函数。
下图显示了 Ionic Page 生命周期事件的顺序。
每个事件的一点解释。
ionViewCanEnter
This event is an Ion Nav Guard Fired before entering into a view(page), allows you to control whether the view can be accessed or not (returning true or false). If the page is already loaded into the memory, this will be the first event to be fired. If not, This method will be fired after the constructor.
ionViewDidLoad
Fired only when a view is stored in memory. This event is NOT fired on entering a view that is already cached. The difference between the constructor and IonViewDidLoad is that the constructor should be as thin as possible, meaning you should only be initializing the variables of your class. This is because making calls to REST API and initializing data in the constructor would cause the application to slow down as the creation of the object takes time. Whereas in ionViewDidLoad event you can be sure that all your variables & dependencies injected are available for use and it's also a good place to make REST API calls and initialize the data.
ionViewWillEnter
This event is fired when entering a page before it becomes the active one. You can use this event for tasks that has to be done every time when a view is entered (ex: Updating a table).
ionViewDidEnter
Fired when entering a page, after it becomes the active page.
ionViewCanLeave
This event is also nav guard like ionViewCanEnter. This event is fired before leaving a view, allows you to control whether the view can be left or not.
ionViewWillLeave
This event is fired when you leave a page. At this stage, your page is still in an active state but it has been queued up to be removed and you can no longer prevent the page from being transitioned away.
ionViewDidLeave
At this state, the page has become inactive. It is important to note that this event will be fired after ionViewDidEnter of the next page has fired. You can use this method to save data or state that can be used when this page is visited again ( Only if the page is not destroyed).
ionViewDidUnload
Fired when a view is going to be completely removed (after leaving a non-cached view). This method can be used to free up the resources that is no longer required
参考:https://blog.ionicframework.com/navigating-lifecycle-events/