Search code examples
vue.jsionic5vuejs3

Ionic 5, Vue 3 - How to use Ionic lifecycle hooks inside setup()?


The Ionic documentation describes how to use the Ionic lifecycle methods like ionViewWillEnter, ionViewDidEnter etc. inside Vue method.

https://ionicframework.com/docs/vue/lifecycle

I'm looking for a way to access them inside the new Vue 3 setup() method so that I can able to access the properties defined there. Is it something possible?

export default defineComponent({
   ...
   setup(){
      const list = ref([]);

      // I need something like this
      const ionViewDidEnter = () => {
         list.value.push(...['some', 'array', 'here']);
      },

      return {
         list,
         ionViewDidEnter
      };
   }
});

Solution

  • This is now possible in a composition API style since this PR was merged

    https://github.com/ionic-team/ionic-framework/pull/22970

    export default defineComponent({
      ...,
      components: { IonPage },
      setup() {
        onIonViewDidEnter(() => {
          console.log('ionViewDidEnter!');
        });
      }
    });