Search code examples
javascriptvue.jsvuejs3vue-routervue-router4

Is it better to use RouterLink or router.push for a button?


I am using Vue Router 4 in Vue 3 Composition API, and I want to make a back button. I want to know what is the most optimal and good practice to redirect, a RouterLink or the router with the push method..

RouterLink

    <RouterLink :to="{ name: 'clients-index' }">
      <AppButton text="Volver" icons="fa-chevron-left" />
    </RouterLink>

router.push

    <AppButton text="Volver" icons="fa-chevron-left" @click="() => router.push({ name: 'clients' })" />

Solution

  • This is clearly written inside the documentation that clicking <router-link :to="..."> is the equivalent of calling router.push(...).

    In router-link we create anchor tags for declarative navigation and the same job can be done programmatically using the router's instance, router.push. To navigate to a different URL we use router.push and this is the same method called internally when you click a <router-link>.

    Declarative Programmatic
    <router-link :to="..."> router.push(...)

    So use either a declarative or programmatic approach, both are equalized.