본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

뒤로 가기 버튼 (ion-back-button)

원문 보기 위키 갱신

뒤로 가기 버튼(Back Button)은 클릭하면 앱의 히스토리에서 이전으로 돌아가는 버튼이에요. defaultHref가 설정되지 않았다면 내비게이션 스택에 히스토리가 있을 때만 표시돼요. 모드에 따라 다른 텍스트와 아이콘을 보여 주지만, 이는 커스터마이즈할 수 있어요.

출처: 문서

본문

Encapsulation: shadow

뒤로 가기 버튼은 클릭하면 앱의 히스토리에서 이전으로 돌아가요. defaultHref가 설정되지 않았다면 내비게이션 스택에 히스토리가 있을 때만 표시돼요. 뒤로 가기 버튼은 모드에 따라 다른 텍스트와 아이콘을 표시하지만, 이것은 커스터마이즈할 수 있어요.

Basic Usage

JavaScript

<ion-app>
  <ion-nav root="page-one"></ion-nav>
</ion-app>

<script>
  class PageOne extends HTMLElement {
    connectedCallback() {
      this.innerHTML = `
      <ion-header>
        <ion-toolbar>
          <ion-title>Back Button</ion-title>
        </ion-toolbar>
      </ion-header>
      <ion-content class="ion-padding">
        <h1>Page One</h1>
        <p>Navigate to the next page to see the back button.</p>
        <ion-nav-link router-direction="forward" component="page-two">
          <ion-button>Navigate</ion-button>
        </ion-nav-link>
      </ion-content>
    `;
    }
  }

  class PageTwo extends HTMLElement {
    connectedCallback() {
      this.innerHTML = `
      <ion-header>
        <ion-toolbar>
          <ion-buttons slot="start">
            <ion-back-button></ion-back-button>
          </ion-buttons>
          <ion-title>Back Button</ion-title>
        </ion-toolbar>
      </ion-header>
      <ion-content class="ion-padding">
        <h1>Page Two</h1>
        <p>Use the back button to navigate to the previous page.</p>
      </ion-content>
    `;
    }
  }

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

Angular

src/app/example.component.html

<ion-nav [root]="component"></ion-nav>

src/app/example.component.ts

import { Component } from '@angular/core';
import { IonNav } from '@ionic/angular';

import { PageOneComponent } from './page-one.component';

@Component({
  selector: 'app-example',
  templateUrl: 'example.component.html',
  styleUrls: ['example.component.css'],
  imports: [IonNav],
})
export class ExampleComponent {
  component = PageOneComponent;
}

src/app/page-one.component.ts

import { Component } from '@angular/core';
import { IonButton, IonContent, IonHeader, IonNavLink, IonTitle, IonToolbar } from '@ionic/angular';

import { PageTwoComponent } from './page-two.component';

@Component({
  selector: 'app-page-one',
  template: `
    <ion-header>
      <ion-toolbar>
        <ion-title>Back Button</ion-title>
      </ion-toolbar>
    </ion-header>
    <ion-content class="ion-padding">
      <h1>Page One</h1>
      <p>Navigate to the next page to see the back button.</p>
      <ion-nav-link router-direction="forward" [component]="component">
        <ion-button>Navigate</ion-button>
      </ion-nav-link>
    </ion-content>
  `,
  imports: [IonButton, IonContent, IonHeader, IonNavLink, IonTitle, IonToolbar],
})
export class PageOneComponent {
  component = PageTwoComponent;
}

src/app/page-two.component.ts

import { Component } from '@angular/core';
import { IonBackButton, IonButtons, IonContent, IonHeader, IonTitle, IonToolbar } from '@ionic/angular';

@Component({
  selector: 'app-page-two',
  template: `
    <ion-header>
      <ion-toolbar>
        <ion-buttons slot="start">
          <ion-back-button></ion-back-button>
        </ion-buttons>
        <ion-title>Back Button</ion-title>
      </ion-toolbar>
    </ion-header>
    <ion-content class="ion-padding">
      <h1>Page Two</h1>
      <p>Use the back button to navigate to the previous page.</p>
    </ion-content>
  `,
  imports: [IonBackButton, IonButtons, IonContent, IonHeader, IonTitle, IonToolbar],
})
export class PageTwoComponent {}

React

src/main.tsx

import React from 'react';
import { IonNav } from '@ionic/react';

import PageOne from './page-one';

function Example() {
  return <IonNav root={() => <PageOne />}></IonNav>;
}
export default Example;

src/page-one.tsx

import React from 'react';
import { IonButton, IonHeader, IonContent, IonNavLink, IonToolbar, IonTitle } from '@ionic/react';

import PageTwo from './page-two';

function PageOne() {
  return (
    <>
      <IonHeader>
        <IonToolbar>
          <IonTitle>Back Button</IonTitle>
        </IonToolbar>
      </IonHeader>
      <IonContent className="ion-padding">
        <h1>Page One</h1>
        <p>Navigate to the next page to see the back button.</p>
        <IonNavLink routerDirection="forward" component={() => <PageTwo />}>
          <IonButton>Navigate</IonButton>
        </IonNavLink>
      </IonContent>
    </>
  );
}

export default PageOne;

src/page-two.tsx

import React from 'react';
import { IonBackButton, IonButtons, IonHeader, IonContent, IonToolbar, IonTitle } from '@ionic/react';

function PageTwo() {
  return (
    <>
      <IonHeader>
        <IonToolbar>
          <IonButtons slot="start">
            <IonBackButton></IonBackButton>
          </IonButtons>
          <IonTitle>Back Button</IonTitle>
        </IonToolbar>
      </IonHeader>
      <IonContent className="ion-padding">
        <h1>Page Two</h1>
        <p>Use the back button to navigate to the previous page.</p>
      </IonContent>
    </>
  );
}

export default PageTwo;

Vue

src/components/Example.vue

<template>
  <ion-nav :root="component"></ion-nav>
</template>

<script setup lang="ts">
  import { IonNav } from '@ionic/vue';
  import PageOne from './PageOne.vue';

  const component = PageOne;
</script>

src/components/PageOne.vue

<template>
  <ion-header>
    <ion-toolbar>
      <ion-title>Back Button</ion-title>
    </ion-toolbar>
  </ion-header>
  <ion-content class="ion-padding">
    <h1>Page One</h1>
    <p>Navigate to the next page to see the back button.</p>
    <ion-nav-link router-direction="forward" :component="component">
      <ion-button>Navigate</ion-button>
    </ion-nav-link>
  </ion-content>
</template>

<script setup lang="ts">
  import { IonButton, IonContent, IonHeader, IonNavLink, IonPage, IonTitle, IonToolbar } from '@ionic/vue';
  import PageTwo from './PageTwo.vue';

  const component = PageTwo;
</script>

src/components/PageTwo.vue

<template>
  <ion-header>
    <ion-toolbar>
      <ion-buttons slot="start">
        <ion-back-button></ion-back-button>
      </ion-buttons>
      <ion-title>Back Button</ion-title>
    </ion-toolbar>
  </ion-header>
  <ion-content class="ion-padding">
    <h1>Page Two</h1>
    <p>Use the back button to navigate to the previous page.</p>
  </ion-content>
</template>

<script setup lang="ts">
  import { IonBackButton, IonButtons, IonContent, IonHeader, IonTitle, IonToolbar } from '@ionic/vue';
</script>

Custom Back Button

기본적으로 뒤로 가기 버튼은 ios에서는 "chevron-back" 아이콘과 함께 "Back" 텍스트를, md에서는 "arrow-back-sharp" 아이콘을 표시해요. 각 뒤로 가기 버튼 컴포넌트에서 icon이나 text 프로퍼티를 설정해 커스터마이즈할 수 있어요. 또는 전역 설정(global config)의 backButtonIcon이나 backButtonText 프로퍼티를 사용해 전역적으로 설정할 수도 있어요. 자세한 내용은 Config 문서를 참고해 주세요.

JavaScript

<ion-app>
  <ion-nav root="page-one"></ion-nav>
</ion-app>

<script>
  class PageOne extends HTMLElement {
    connectedCallback() {
      this.innerHTML = `
      <ion-header>
        <ion-toolbar>
          <ion-title>Back Button</ion-title>
        </ion-toolbar>
      </ion-header>
      <ion-content class="ion-padding">
        <h1>Page One</h1>
        <p>Navigate to the next page to see the back button.</p>
        <ion-nav-link router-direction="forward" component="page-two">
          <ion-button>Navigate</ion-button>
        </ion-nav-link>
      </ion-content>
    `;
    }
  }

  class PageTwo extends HTMLElement {
    connectedCallback() {
      this.innerHTML = `
      <ion-header>
        <ion-toolbar>
          <ion-buttons slot="start">
            <ion-back-button text="Previous" icon="caret-back"></ion-back-button>
          </ion-buttons>
          <ion-title>Back Button</ion-title>
        </ion-toolbar>
      </ion-header>
      <ion-content class="ion-padding">
        <h1>Page Two</h1>
        <p>Use the back button to navigate to the previous page.</p>
      </ion-content>
    `;
    }
  }

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

Angular

src/app/example.component.html

<ion-nav [root]="component"></ion-nav>

src/app/example.component.ts

import { Component } from '@angular/core';
import { IonNav } from '@ionic/angular';

import { PageOneComponent } from './page-one.component';

@Component({
  selector: 'app-example',
  templateUrl: 'example.component.html',
  styleUrls: ['example.component.css'],
  imports: [IonNav],
})
export class ExampleComponent {
  component = PageOneComponent;
}

src/app/page-one.component.ts

import { Component } from '@angular/core';
import { IonButton, IonContent, IonHeader, IonNavLink, IonTitle, IonToolbar } from '@ionic/angular';

import { PageTwoComponent } from './page-two.component';

@Component({
  selector: 'app-page-one',
  template: `
    <ion-header>
      <ion-toolbar>
        <ion-title>Back Button</ion-title>
      </ion-toolbar>
    </ion-header>
    <ion-content class="ion-padding">
      <h1>Page One</h1>
      <p>Navigate to the next page to see the back button.</p>
      <ion-nav-link router-direction="forward" [component]="component">
        <ion-button>Navigate</ion-button>
      </ion-nav-link>
    </ion-content>
  `,
  imports: [IonButton, IonContent, IonHeader, IonNavLink, IonTitle, IonToolbar],
})
export class PageOneComponent {
  component = PageTwoComponent;
}

src/app/page-two.component.ts

import { Component } from '@angular/core';
import { IonBackButton, IonButtons, IonContent, IonHeader, IonTitle, IonToolbar } from '@ionic/angular';

import { addIcons } from 'ionicons';
import { caretBack } from 'ionicons/icons';

@Component({
  selector: 'app-page-two',
  template: `
    <ion-header>
      <ion-toolbar>
        <ion-buttons slot="start">
          <ion-back-button text="Previous" icon="caret-back"></ion-back-button>
        </ion-buttons>
        <ion-title>Back Button</ion-title>
      </ion-toolbar>
    </ion-header>
    <ion-content class="ion-padding">
      <h1>Page Two</h1>
      <p>Use the back button to navigate to the previous page.</p>
    </ion-content>
  `,
  imports: [IonBackButton, IonButtons, IonContent, IonHeader, IonTitle, IonToolbar],
})
export class PageTwoComponent {
  constructor() {
    /**
     * Any icons you want to use in your application
     * can be registered in app.component.ts and then
     * referenced by name anywhere in your application.
     */
    addIcons({ caretBack });
  }
}

React

src/main.tsx

import React from 'react';
import { IonNav } from '@ionic/react';

import PageOne from './page-one';

function Example() {
  return <IonNav root={() => <PageOne />}></IonNav>;
}
export default Example;

src/page-one.tsx

import React from 'react';
import { IonButton, IonHeader, IonContent, IonNavLink, IonToolbar, IonTitle } from '@ionic/react';

import PageTwo from './page-two';

function PageOne() {
  return (
    <>
      <IonHeader>
        <IonToolbar>
          <IonTitle>Back Button</IonTitle>
        </IonToolbar>
      </IonHeader>
      <IonContent className="ion-padding">
        <h1>Page One</h1>
        <p>Navigate to the next page to see the back button.</p>
        <IonNavLink routerDirection="forward" component={() => <PageTwo />}>
          <IonButton>Navigate</IonButton>
        </IonNavLink>
      </IonContent>
    </>
  );
}

export default PageOne;

src/page-two.tsx

import React from 'react';
import { IonBackButton, IonButtons, IonHeader, IonContent, IonToolbar, IonTitle } from '@ionic/react';
import { caretBack } from 'ionicons/icons';

function PageTwo() {
  return (
    <>
      <IonHeader>
        <IonToolbar>
          <IonButtons slot="start">
            <IonBackButton text="Previous" icon={caretBack}></IonBackButton>
          </IonButtons>
          <IonTitle>Back Button</IonTitle>
        </IonToolbar>
      </IonHeader>
      <IonContent className="ion-padding">
        <h1>Page Two</h1>
        <p>Use the back button to navigate to the previous page.</p>
      </IonContent>
    </>
  );
}

export default PageTwo;

Vue

src/components/Example.vue

<template>
  <ion-nav :root="component"></ion-nav>
</template>

<script setup lang="ts">
  import { IonNav } from '@ionic/vue';
  import PageOne from './PageOne.vue';

  const component = PageOne;
</script>

src/components/PageOne.vue

<template>
  <ion-header>
    <ion-toolbar>
      <ion-title>Back Button</ion-title>
    </ion-toolbar>
  </ion-header>
  <ion-content class="ion-padding">
    <h1>Page One</h1>
    <p>Navigate to the next page to see the back button.</p>
    <ion-nav-link router-direction="forward" :component="component">
      <ion-button>Navigate</ion-button>
    </ion-nav-link>
  </ion-content>
</template>

<script setup lang="ts">
  import { IonHeader, IonTitle, IonToolbar, IonContent, IonNavLink, IonButton } from '@ionic/vue';
  import PageTwo from './PageTwo.vue';

  const component = PageTwo;
</script>

src/components/PageTwo.vue

<template>
  <ion-header>
    <ion-toolbar>
      <ion-buttons slot="start">
        <ion-back-button text="Previous" :icon="caretBack"></ion-back-button>
      </ion-buttons>
      <ion-title>Back Button</ion-title>
    </ion-toolbar>
  </ion-header>
  <ion-content class="ion-padding">
    <h1>Page Two</h1>
    <p>Use the back button to navigate to the previous page.</p>
  </ion-content>
</template>

<script setup lang="ts">
  import { IonHeader, IonTitle, IonToolbar, IonContent, IonButtons, IonBackButton } from '@ionic/vue';
  import { caretBack } from 'ionicons/icons';
</script>

Default Back History

가끔 앱은 히스토리가 없을 때도 뒤로 가기 버튼을 보여 주고 이전으로 돌아가야 할 때가 있어요. 이 경우 뒤로 가기 버튼의 defaultHref를 경로로 설정하면 돼요. defaultHref를 사용하려면 앱에 경로가 설정된 라우터가 있어야 해요.

Properties

color

Description: The color to use from your application's color palette. Default options are: "primary", "secondary", "tertiary", "success", "warning", "danger", "light", "medium", and "dark". For more information on colors, see theming.

Attribute: color

Type: "danger" | "dark" | "light" | "medium" | "primary" | "secondary" | "success" | "tertiary" | "warning" | string | undefined

Default: undefined

defaultHref

Description: The url to navigate back to by default when there is no history.

Attribute: default-href

Type: string | undefined

Default: undefined

disabled

Description: If true, the user cannot interact with the button.

Attribute: disabled

Type: boolean

Default: false

icon

Description: The built-in named SVG icon name or the exact src of an SVG file to use for the back button.

Attribute: icon

Type: null | string | undefined

Default: undefined

mode

Description: The mode determines which platform styles to use. This is a virtual property that is set once during initialization and will not update if you change its value after the initial render.

Attribute: mode

Type: "ios" | "md"

Default: undefined

routerAnimation

Description: When using a router, it specifies the transition animation when navigating to another page.

Attribute: undefined

Type: ((baseEl: any, opts?: any) => Animation) | undefined

Default: undefined

text

Description: The text to display in the back button.

Attribute: text

Type: null | string | undefined

Default: undefined

type

Description: The type of the button.

Attribute: type

Type: "button" | "reset" | "submit"

Default: 'button'

Events

No events available for this component.

Methods

No public methods available for this component.

CSS Shadow Parts

Name Description
icon The back button icon (uses ion-icon).
native The native HTML button element that wraps all child elements.
text The back button text.

CSS Custom Properties

iOS

Name Description
--background Background of the button
--background-focused Background of the button when focused with the tab key
--background-focused-opacity Opacity of the button background when focused with the tab key
--background-hover Background of the button on hover
--background-hover-opacity Opacity of the background on hover
--border-radius Border radius of the button
--color Text color of the button
--color-focused Text color of the button when focused with the tab key
--color-hover Text color of the button on hover
--icon-font-size Font size of the button icon
--icon-font-weight Font weight of the button icon
--icon-margin-bottom Bottom margin of the button icon
--icon-margin-end Right margin if direction is left-to-right, and left margin if direction is right-to-left of the button icon
--icon-margin-start Left margin if direction is left-to-right, and right margin if direction is right-to-left of the button icon
--icon-margin-top Top margin of the button icon
--icon-padding-bottom Bottom padding of the button icon
--icon-padding-end Right padding if direction is left-to-right, and left padding if direction is right-to-left of the button icon
--icon-padding-start Left padding if direction is left-to-right, and right padding if direction is right-to-left of the button icon
--icon-padding-top Top padding of the button icon
--margin-bottom Bottom margin of the button
--margin-end Right margin if direction is left-to-right, and left margin if direction is right-to-left of the button
--margin-start Left margin if direction is left-to-right, and right margin if direction is right-to-left of the button
--margin-top Top margin of the button
--min-height Minimum height of the button
--min-width Minimum width of the button
--opacity Opacity of the button
--padding-bottom Bottom padding of the button
--padding-end Right padding if direction is left-to-right, and left padding if direction is right-to-left of the button
--padding-start Left padding if direction is left-to-right, and right padding if direction is right-to-left of the button
--padding-top Top padding of the button
--ripple-color Color of the button ripple effect
--transition Transition of the button

MD

Name Description
--background Background of the button
--background-focused Background of the button when focused with the tab key
--background-focused-opacity Opacity of the button background when focused with the tab key
--background-hover Background of the button on hover
--background-hover-opacity Opacity of the background on hover
--border-radius Border radius of the button
--color Text color of the button
--color-focused Text color of the button when focused with the tab key
--color-hover Text color of the button on hover
--icon-font-size Font size of the button icon
--icon-font-weight Font weight of the button icon
--icon-margin-bottom Bottom margin of the button icon
--icon-margin-end Right margin if direction is left-to-right, and left margin if direction is right-to-left of the button icon
--icon-margin-start Left margin if direction is left-to-right, and right margin if direction is right-to-left of the button icon
--icon-margin-top Top margin of the button icon
--icon-padding-bottom Bottom padding of the button icon
--icon-padding-end Right padding if direction is left-to-right, and left padding if direction is right-to-left of the button icon
--icon-padding-start Left padding if direction is left-to-right, and right padding if direction is right-to-left of the button icon
--icon-padding-top Top padding of the button icon
--margin-bottom Bottom margin of the button
--margin-end Right margin if direction is left-to-right, and left margin if direction is right-to-left of the button
--margin-start Left margin if direction is left-to-right, and right margin if direction is right-to-left of the button
--margin-top Top margin of the button
--min-height Minimum height of the button
--min-width Minimum width of the button
--opacity Opacity of the button
--padding-bottom Bottom padding of the button
--padding-end Right padding if direction is left-to-right, and left padding if direction is right-to-left of the button
--padding-start Left padding if direction is left-to-right, and right padding if direction is right-to-left of the button
--padding-top Top padding of the button
--ripple-color Color of the button ripple effect
--transition Transition of the button

Slots

No slots available for this component.

더 알아보기 (Learn more)

ion-back-button은 내비게이션 스택의 히스토리가 있을 때 이전 페이지로 돌아가는 버튼이에요. 히스토리가 없을 때도 표시하고 싶다면 defaultHref를 설정하고, 모양은 icon과 text 프로퍼티 또는 전역 설정으로 바꿀 수 있어요. 내비게이션 버튼들을 묶어 두는 방법은 ion-buttons 문서를 참고하세요.