Zum Inhalt springen
Components

ion-router

The router is a component for handling routing inside vanilla and Stencil JavaScript projects.

note

Note: This component should only be used with vanilla and Stencil JavaScript projects. See the routing guides for Angular, React, and Vue for framework-specific routing solutions.

Apps should have a single ion-router component in the codebase. This component controls all interactions with the browser history and it aggregates updates through an event system.

ion-router is just a URL coordinator for the navigation outlets of ionic: ion-tabs and ion-router-outlet.

That means the ion-router never touches the DOM, it does NOT show the components or emit any kind of lifecycle events, it just tells ion-tabs and ion-router-outlet what and when to "show" based on the browser's URL.

To configure this relationship between components (to load/select) and URLs, ion-router uses a declarative syntax using JSX/HTML to define a tree of routes.

Basic Usage

<ion-router>
  <ion-route url="/" component="page-one"></ion-route>
  <ion-route url="/page-two" component="page-two"></ion-route>
</ion-router>

<ion-router-outlet></ion-router-outlet>

<script>
  class PageOne extends HTMLElement {
    connectedCallback() {
      this.innerHTML = `
    <ion-header>
      <ion-toolbar>
        <ion-title>Page One</ion-title>
      </ion-toolbar>
    </ion-header>
    <ion-content class="ion-padding">
      This is the content for page 1.
      <ion-router-link href="#/page-two">
        <ion-button>Go to Page 2</ion-button>
      </ion-router-link>
    </ion-content>`;
    }
  }

  class PageTwo extends HTMLElement {
    connectedCallback() {
      this.innerHTML = `
    <ion-header>
      <ion-toolbar>
        <ion-buttons slot="start">
          <ion-back-button default-href="/"></ion-back-button>
        </ion-buttons>
        <ion-title>Page Two</ion-title>
      </ion-toolbar>
    </ion-header>
    <ion-content class="ion-padding">
      This is the content for page 2.
    </ion-content>`;
    }
  }

  customElements.define('page-one', PageOne);
  customElements.define('page-two', PageTwo);
</script>

Using ion-nav within a Routed Page

ion-router and ion-nav are separate systems. ion-router coordinates URL-based navigation through ion-router-outlet, while ion-nav manages a local stack that is independent of the URL. ion-nav does not integrate with ion-router: placing an ion-nav inside an ion-router does not turn it into a routed outlet, and pushing or popping views on an ion-nav never changes the URL.

The two can still be composed. A routed page rendered by ion-router-outlet can host its own ion-nav for local, URL-less navigation within that page. In the example below, navigating to /details updates the URL, but stepping through the ion-nav inside that page does not.

<!-- The router drives URL-based navigation through ion-router-outlet -->
<ion-router>
  <ion-route url="/" component="page-home"></ion-route>
  <ion-route url="/details" component="page-details"></ion-route>
</ion-router>

<ion-router-outlet></ion-router-outlet>

<script>
  // A routed page: navigating here updates the URL to /details.
  class PageHome extends HTMLElement {
    connectedCallback() {
      this.innerHTML = `
    <ion-header>
      <ion-toolbar>
        <ion-title>Home</ion-title>
      </ion-toolbar>
    </ion-header>
    <ion-content class="ion-padding">
      <ion-router-link href="#/details">
        <ion-button>Go to Details (URL changes)</ion-button>
      </ion-router-link>
    </ion-content>`;
    }
  }

  // This routed page hosts its own ion-nav for a local stack.
  // Pushing and popping within the ion-nav does not change the URL.
  class PageDetails extends HTMLElement {
    connectedCallback() {
      this.innerHTML = `<ion-nav root="details-step-one"></ion-nav>`;
    }
  }

  class DetailsStepOne extends HTMLElement {
    connectedCallback() {
      this.innerHTML = `
    <ion-header>
      <ion-toolbar>
        <ion-buttons slot="start">
          <ion-back-button default-href="/"></ion-back-button>
        </ion-buttons>
        <ion-title>Details - Step 1</ion-title>
      </ion-toolbar>
    </ion-header>
    <ion-content class="ion-padding">
      <ion-nav-link router-direction="forward" component="details-step-two">
        <ion-button>Next step (URL stays the same)</ion-button>
      </ion-nav-link>
    </ion-content>`;
    }
  }

  class DetailsStepTwo extends HTMLElement {
    connectedCallback() {
      this.innerHTML = `
    <ion-header>
      <ion-toolbar>
        <ion-buttons slot="start">
          <ion-back-button></ion-back-button>
        </ion-buttons>
        <ion-title>Details - Step 2</ion-title>
      </ion-toolbar>
    </ion-header>
    <ion-content class="ion-padding">
      The URL is still /details. This step is managed by ion-nav, not the router.
    </ion-content>`;
    }
  }

  customElements.define('page-home', PageHome);
  customElements.define('page-details', PageDetails);
  customElements.define('details-step-one', DetailsStepOne);
  customElements.define('details-step-two', DetailsStepTwo);
</script>

Interfaces

RouterEventDetail

interface RouterEventDetail {
  from: string | null;
  redirectedFrom: string | null;
  to: string;
}

RouterCustomEvent

While not required, this interface can be used in place of the CustomEvent interface for stronger typing with Ionic events emitted from this component.

interface RouterCustomEvent extends CustomEvent {
  detail: RouterEventDetail;
  target: HTMLIonRouterElement;
}

Usage

<ion-router>
  <ion-route component="page-tabs">
    <ion-route url="/schedule" component="tab-schedule">
      <ion-route component="page-schedule"></ion-route>
      <ion-route url="/session/:sessionId" component="page-session"></ion-route>
    </ion-route>

    <ion-route url="/speakers" component="tab-speaker">
      <ion-route component="page-speaker-list"></ion-route>
      <ion-route url="/session/:sessionId" component="page-session"></ion-route>
      <ion-route url="/:speakerId" component="page-speaker-detail"></ion-route>
    </ion-route>

    <ion-route url="/map" component="page-map"></ion-route>
    <ion-route url="/about" component="page-about"></ion-route>
  </ion-route>

  <ion-route url="/tutorial" component="page-tutorial"></ion-route>
  <ion-route url="/login" component="page-login"></ion-route>
  <ion-route url="/account" component="page-account"></ion-route>
  <ion-route url="/signup" component="page-signup"></ion-route>
  <ion-route url="/support" component="page-support"></ion-route>
</ion-router>

Properties

root

DescriptionThe root path to use when matching URLs. By default, this is set to "/", but you can specify an alternate prefix for all URL paths.
Attributeroot
Typestring
Default'/'

useHash

DescriptionThe router can work in two "modes": - With hash: /index.html#/path/to/page - Without hash: /path/to/page

Using one or another might depend in the requirements of your app and/or where it's deployed.

Usually "hash-less" navigation works better for SEO and it's more user friendly too, but it might requires additional server-side configuration in order to properly work.

On the other side hash-navigation is much easier to deploy, it even works over the file protocol.

By default, this property is true, change to false to allow hash-less URLs.
Attributeuse-hash
Typeboolean
Defaulttrue

Events

NameDescriptionBubbles
ionRouteDidChangeEmitted when the route had changedtrue
ionRouteWillChangeEvent emitted when the route is about to changetrue

Methods

back

DescriptionGo back to previous page in the window.history.
Signatureback() => Promise<void>

push

DescriptionNavigate to the specified path.
Signaturepush(path: string, direction?: RouterDirection, animation?: AnimationBuilder) => Promise<boolean>
Parameterspath: The path to navigate to.
direction: The direction of the animation. Defaults to "forward".
animation: A custom animation to use for the transition.

CSS Shadow Parts

No CSS shadow parts available for this component.

CSS Custom Properties

No CSS custom properties available for this component.

Slots

No slots available for this component.