ion-router
vanilla와 Stencil JavaScript 프로젝트 안에서 라우팅을 처리하는 컴포넌트예요. 브라우저 URL을 바탕으로 Ionic의 내비게이션 아울렛에게 무엇을, 언제 "보여줄지" 알려주는 URL 조정자(coordinator)입니다.
출처: 문서
본문
라우터는 vanilla와 Stencil JavaScript 프로젝트 안에서 라우팅을 처리하는 컴포넌트예요.
Note
참고: 이 컴포넌트는 vanilla와 Stencil JavaScript 프로젝트에서만 사용해야 해요. 프레임워크별 라우팅 솔루션은 Angular, React, Vue 라우팅 가이드를 참고하세요.
앱은 코드베이스에 ion-router 컴포넌트를 하나만 가져야 해요. 이 컴포넌트는 브라우저 기록과의 모든 상호작용을 제어하고, 이벤트 시스템을 통해 업데이트를 모아 전달합니다.
ion-router는 Ionic의 내비게이션 아울렛(ion-tabs와 ion-router-outlet)을 위한 URL 조정자일 뿐이에요.
즉 ion-router는 DOM을 절대 건드리지 않아요. 컴포넌트를 보여주지도 않고 어떤 생명주기 이벤트도 발생시키지 않으며, 그저 브라우저 URL을 바탕으로 ion-tabs와 ion-router-outlet에게 무엇을, 언제 "보여줄지"만 알려줍니다.
컴포넌트(로드/선택)와 URL 사이의 이 관계를 구성하기 위해, ion-router는 JSX/HTML을 이용한 선언적 구문으로 라우트의 트리를 정의합니다.
Basic Usage
JavaScript
<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와 ion-nav는 별개의 시스템이에요. ion-router는 ion-router-outlet을 통해 URL 기반 내비게이션을 조정하고, ion-nav는 URL과 무관한 로컬 스택을 관리합니다. ion-nav는 ion-router와 통합되지 않아요. ion-router 안에 ion-nav를 넣는다고 해서 라우트된 아울렛이 되는 것이 아니며, ion-nav에서 뷰를 push하거나 pop해도 URL은 절대 바뀌지 않습니다.
그래도 둘은 함께 구성할 수 있어요. ion-router-outlet이 렌더링한 라우트된 페이지는 그 페이지 안에서 URL 없이 로컬 내비게이션을 하기 위한 자체 ion-nav를 가질 수 있습니다. 아래 예시에서 /details로 이동하면 URL이 바뀌지만, 페이지 안의 ion-nav를 단계별로 넘어가는 것은 URL에 영향을 주지 않아요.
JavaScript
<!-- 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
필수는 아니지만, 이 컴포넌트에서 발생하는 Ionic 이벤트에 대해 더 강력한 타입을 제공하려면 CustomEvent 인터페이스 대신 이 인터페이스를 사용할 수 있어요.
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
Description: URL을 매칭할 때 사용할 루트 경로예요. 기본값은 "/"이지만, 모든 URL 경로의 접두사를 다르게 지정할 수 있어요.
Attribute: root
Type: string
Default: '/'
useHash
Description: 라우터는 다음 두 "모드"로 동작할 수 있어요: - 해시 사용: /index.html#/path/to/page - 해시 없음: /path/to/page 둘 중 무엇을 쓸지는 앱의 요구사항이나 배포 위치에 따라 달라질 수 있습니다. 보통 "해시 없는" 내비게이션은 SEO에 더 좋고 사용자에게도 더 친숙하지만, 제대로 동작하려면 추가적인 서버 측 설정이 필요할 수 있어요. 반면 해시 내비게이션은 배포가 훨씬 쉽고 파일 프로토콜에서도 동작합니다. 기본적으로 이 프로퍼티의 값은 true이며, 해시 없는 URL을 허용하려면 false로 바꾸세요.
Attribute: use-hash
Type: boolean
Default: true
Events
| Name | Description | Bubbles |
|---|---|---|
ionRouteDidChange |
라우트가 변경되었을 때 발생해요. | true |
ionRouteWillChange |
라우트가 변경되려 할 때 발생해요. | true |
Methods
back
Description: window.history에서 이전 페이지로 돌아갑니다.
Signature: back() => Promise<void>
push
Description: 지정된 경로로 내비게이션해요.
Signature: push(path: string, direction?: RouterDirection, animation?: AnimationBuilder) => Promise<boolean>
Parameters: path: 이동할 경로예요. direction: 애니메이션의 방향이에요. 기본값은 "forward"입니다. animation: 전환에 사용할 커스텀 애니메이션이에요.
CSS Shadow Parts
이 컴포넌트에는 사용할 수 있는 CSS shadow part가 없어요.
CSS Custom Properties
이 컴포넌트에는 사용할 수 있는 CSS 커스텀 프로퍼티가 없어요.
Slots
이 컴포넌트에는 사용할 수 있는 슬롯이 없어요.
더 알아보기 (Learn more)
ion-router는 브라우저 URL과 Ionic 내비게이션 아울렛을 연결하는 중추 역할을 해요. ion-route로 화면을 선언하고, ion-router-outlet이 실제 화면을 렌더링합니다. 각 역할은 ion-route와 ion-router-outlet 문서에서 함께 다뤄 보세요.