A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://ionicframework.com/docs/vue/lifecycle below:

Vue Lifecycle | Ionic Framework

Vue Lifecycle

This guide discusses how to use the Ionic Framework Lifecycle events in an Ionic Vue application.

Ionic Framework provides a few lifecycle methods that you can use in your apps:

Event Name Description ionViewWillEnter Fired when the component routing to is about to animate into view. ionViewDidEnter Fired when the component routing to has finished animating. ionViewWillLeave Fired when the component routing from is about to animate. ionViewDidLeave Fired when the component routing from has finished animating.

These lifecycles are only called on components directly mapped by a router. This means if /pageOne maps to PageOneComponent, then Ionic lifecycles will be called on PageOneComponent but will not be called on any child components that PageOneComponent may render.

The lifecycles are defined the same way Vue lifecycle methods are - as functions at the root of your Vue component:

import { IonPage } from '@ionic/vue';
import { defineComponent } from 'vue';

export default defineComponent({
name: 'Home',
ionViewDidEnter() {
console.log('Home page did enter');
},
ionViewDidLeave() {
console.log('Home page did leave');
},
ionViewWillEnter() {
console.log('Home page will enter');
},
ionViewWillLeave() {
console.log('Home page will leave');
},
components: {
IonPage,
},
});
Composition API Hooks

These lifecycles can also be expressed using Vue 3's Composition API:

import { IonPage, onIonViewWillEnter, onIonViewDidEnter, onIonViewWillLeave, onIonViewDidLeave } from '@ionic/vue';
import { defineComponent } from 'vue';

export default defineComponent({
name: 'Home',
components: {
IonPage,
},
setup() {
onIonViewDidEnter(() => {
console.log('Home page did enter');
});

onIonViewDidLeave(() => {
console.log('Home page did leave');
});

onIonViewWillEnter(() => {
console.log('Home page will enter');
});

onIonViewWillLeave(() => {
console.log('Home page will leave');
});
},
});

note

Pages in your app need to be using the IonPage component in order for lifecycle methods and hooks to fire properly.

How Ionic Framework Handles the Life of a Page

Ionic Framework has its router outlet, called <ion-router-outlet>. This outlet extends Vue Router's <router-view> with some additional functionality to enable better experiences for mobile devices.

When an app is wrapped in <ion-router-outlet>, Ionic Framework treats navigation a bit differently. When you navigate to a new page, Ionic Framework will keep the old page in the existing DOM, but hide it from your view and transition the new page. The reason we do this is two-fold:

  1. We can maintain the state of the old page (data on the screen, scroll position, etc...).
  2. We can provide a smoother transition back to the page since it is already there and does not need to be created.

Pages are only removed from the DOM when they are "popped", for instance, by pressing the back button in the UI or the browsers back button.

Because of this special handling, certain Vue Router components such as <keep-alive>, <transition>, and <router-view> should not be used in Ionic Vue applications. Additionally, Vue Router's Scroll Behavior API is not needed here as each page's scroll position is preserved automatically.

All the lifecycle methods in Vue (mounted, beforeUnmount, etc..) are available for you to use as well. However, since Ionic Vue manages the lifetime of a page, certain events might not fire when you expect them to. For instance, mounted fires the first time a page is displayed, but if you navigate away from the page Ionic Framework might keep the page around in the DOM, and a subsequent visit to the page might not call mounted again. This scenario is the main reason the Ionic Framework lifecycle methods exist, to still give you a way to call logic when views enter and exit when the native framework's events might not fire.

Below are some tips on use cases for each of the life cycle events.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4