ionic icon indicating copy to clipboard operation
ionic copied to clipboard

Tab not marked as selected on initial route

Open DepkaCZ opened this issue 1 year ago • 5 comments

📚 What are you trying to do?

Hi,

according to docs you should set alias to / on the tab you wish to render instead of having index.vue in pages directory. That works fine as the tab gets rendered but the button in <ion-tab-bar> is not marked as selected. This doesn't work even in the modules playground project.

Does anyone know please how to fix that other than checking for route programatically and setting selected to true ?

🔍 What have you tried?

I've tried to change the href on <ion-tab-button> to / but that completely ruined the active tab detection. Furthermore, can some please explain to me how does it even work please ? Like ...when i set alias in define page meta on specific tab how does it know to render the parent component as well ? It's pretty unclear from the docs :(

ℹ️ Additional context

No response

DepkaCZ avatar Jun 04 '24 07:06 DepkaCZ

I have the same problem! As a workaround I created an index page outside my tabs and in the onBeforeMounted i redirect the user to the tabs/home: router.replace('/tabs/home');

This was working fine until the new update. Now it will not be redirect to the /tabs/home and I don't know why...

buddy94 avatar Jun 06 '24 07:06 buddy94

@DepkaCZ Can you drop a link to your current reproduction

chibx avatar Jun 13 '24 09:06 chibx

@CHIBX Hi, just run the playground and navigate to / route

DepkaCZ avatar Jun 13 '24 11:06 DepkaCZ

I was having the same issue

Ionic's documentation suggests redirecting from / to /home when home tab location is /home.

Example from Usage with Router - ionicframework.com:

// src/router.ts

const routes: Array<RouteRecordRaw> = [
    { path: '/', redirect: '/home', },
    {
      path: '/',
      component: Tabs,
      children: [
        { path: '', redirect: '/home', },
        { path: 'home', component: () => import('./views/HomePage.vue'), },
    }
]

What helped me is creating pages/index.vue and using redirect in definePageMeta to redirect to the home tab:

<!-- pages/index.vue -->
<script lang="ts" setup>
definePageMeta({
  redirect: "/tabs/home",
});
</script>
<!-- pages/tabs/home/index.vue -->
<script lang="ts" setup>
definePageMeta({
  alias: ["/tabs"],
});
</script>

This way navigating to / redirects to /tabs/home, and Home tab is correctly shown as active on initial route

nWacky avatar Sep 30 '24 15:09 nWacky

This issue has an other symptom: if you start navigation from /, go to a sub-page of the home tab, then to another tab, and then back to home tab, you will land on the home page instead of the sub-page. In other words, the alias does not handle properly stacked navigation history.

I reproduce it on the playground (after adding a sub page to tab1).

rthouvenin avatar Dec 03 '24 13:12 rthouvenin