Skip to content

Commit 3bdc3de

Browse files
authored
fix(vue): prevent tab buttons from navigating without a router outlet (#31834)
Issue number: internal --------- ## What is the current behavior? In Vue, when `IonTabs` has `IonTab` children instead of an `IonRouterOutlet`, clicking the tab that's already selected can navigate the app to a different page. This happens when the router history has an entry for a router-outlet tab with the same `tab` name. For example, the app visits `/tabs/tab1`, pushes a page with routerless tabs, and the user then clicks the selected Tab 1. The URL goes back to `/tabs/tab1`. `IonTabButton` calls `resetTab()` on the router even when there's no router outlet. `resetTab()` finds the other tabs' history entry and navigates back to it. Clicking a different tab doesn't navigate, because `changeTab()` returns early when the path is empty. ## What is the new behavior? - Without a router outlet, `IonTabButton` no longer calls the router, so clicking a tab never changes the URL. Tabs with a router outlet are unchanged. - A new e2e test opens the router-outlet tabs, pushes `/tabs-basic`, clicks the selected tab, and checks that the URL stays the same. ## Does this introduce a breaking change? - [ ] Yes - [x] No ## Other information N/A
1 parent 35281e5 commit 3bdc3de

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

‎packages/vue/src/components/IonTabButton.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,12 @@ export const IonTabButton = /*@__PURE__*/ defineComponent({
7171
* then we must be on a child page and
7272
* should direct users back to the root
7373
* of the tab.
74+
*
75+
* Without a router outlet, the tabs are not
76+
* tied to the router, so the router must not
77+
* navigate when a tab button is clicked.
7478
*/
75-
if (ionRouter !== null) {
79+
if (hasRouterOutlet && ionRouter !== null) {
7680
if (prevActiveTab === tab) {
7781
if (originalHref !== currentHref) {
7882
ionRouter.resetTab(tab, originalHref);

‎packages/vue/test/base/tests/e2e/specs/tabs.cy.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,20 @@ describe('Tabs', () => {
673673

674674
cy.url().should('include', '/tabs-basic');
675675
});
676+
677+
it('should not navigate away when clicking the selected tab after visiting tabs with a router outlet', () => {
678+
cy.routerPush('/tabs');
679+
cy.ionPageVisible('tab1');
680+
681+
cy.routerPush('/tabs-basic');
682+
cy.url().should('include', '/tabs-basic');
683+
cy.get('ion-tab[data-pageid=tab1]').should('be.visible');
684+
685+
cy.get('ion-tabs:has(> ion-tab) ion-tab-button#tab-button-tab1').click();
686+
687+
cy.url().should('include', '/tabs-basic');
688+
cy.get('ion-tab[data-pageid=tab1]').should('be.visible');
689+
});
676690
})
677691

678692
describe('Swipe to Go Back', () => {

0 commit comments

Comments
 (0)