Utility Functions
Ionic Vue ships with several utility functions that you can use in your application to make certain tasks easier such as managing the on-screen keyboard and the hardware back button.
Router
Functions
useIonRouter
▸ useIonRouter(): UseIonRouterResult
Returns the Ionic router instance, containing API methods for navigating, customizing page transitions and routing context for native features. This function can be used in combination with the useRouter
from Vue.
Customizing Page Transitions
import { IonPage, useIonRouter } from '@ionic/vue';
import { defineComponent } from 'vue';
import { customAnimation } from '@/animations/customAnimation';
export default defineComponent({
components: { IonPage },
setup() {
const router = useIonRouter();
const push = () => {
router.push('/page2', customAnimation);
};
const back = () => {
router.back(customAnimation);
};
return { push, back };
},
});
Hardware back button on Android
You may want to know if you are at the root page of the application when a user presses the hardware back button on Android.
import { useIonRouter } from '@ionic/vue';
...
export default {
setup() {
const ionRouter = useIonRouter();
if (ionRouter.canGoBack()) {
// Perform some action here
}
}
}
For additional APIs with Vue routing, please refer to the Vue Router documentation.