Config
Ionic Config は、アプリ全体でコンポーネントのプロパティをグローバルに変更する方法を提供します。アプリモード、タブボタンのレイアウト、アニメーションなどを設定できます。
グローバル設定
利用可能な設定キーは、IonicConfigインターフェースで見つけることができます。
次の例では、リップル効果を無効にし、モードを Material Design にデフォルト設定します:
- JavaScript
- Angular
- Angular (Standalone)
- React
- Vue
window.Ionic = {
config: {
rippleEffect: false,
mode: 'md',
},
};
/*
* IonicModule is deprecated and will be removed in a future major version.
* Refer to the "Angular (Standalone)" tab to use `provideIonicAngular()` instead.
*/
import { IonicModule } from '@ionic/angular/lazy';
@NgModule({
...
imports: [
IonicModule.forRoot({
rippleEffect: false,
mode: 'md'
})
],
...
})
import { provideIonicAngular } from '@ionic/angular';
bootstrapApplication(AppComponent, {
providers: [
...,
provideIonicAngular({
rippleEffect: false,
mode: 'md'
})
]
})
IonApp を含む Ionic コンポーネントを描画する前に、setupIonicReact を呼び出す必要があります。
import { setupIonicReact } from '@ionic/react';
setupIonicReact({
rippleEffect: false,
mode: 'md',
});
import { IonicVue } from '@ionic/vue';
import { createApp } from 'vue';
createApp(App).use(IonicVue, {
rippleEffect: false,
mode: 'md',
});
コンポーネントごとの設定
Ionic Config は reactive ではありません。コンポーネントがレンダリングされた後に設定の値を更新しても、以前の値のままになります。reactive な値が必要な場合は、設定を更新する代わりに、コンポーネントのプロパティを使用することをお勧めします。
- JavaScript
- Angular
- Angular (Standalone)
- React
- Vue
Not recommended
window.Ionic = {
config: {
// Not recommended when your app requires reactive values
backButtonText: 'Go Back',
},
};
Recommended
<ion-back-button></ion-back-button>
<script>
const backButton = document.querySelector('ion-back-button');
/**
* The back button text can be updated
* anytime the locale changes.
*/
backButton.text = 'Go Back';
</script>
Not recommended
/*
* IonicModule is deprecated and will be removed in a future major version.
* Refer to the "Angular (Standalone)" tab to use `provideIonicAngular()` instead.
*/
import { IonicModule } from '@ionic/angular/lazy';
@NgModule({
...
imports: [
IonicModule.forRoot({
// Not recommended when your app requires reactive values
backButtonText: 'Go Back'
})
],
...
});
Recommended
<ion-back-button [text]="backButtonText"></ion-back-button>
@Component(...)
class MyComponent {
/**
* The back button text can be updated
* anytime the locale changes.
*/
backButtonText = 'Go Back';
}
Not recommended
import { provideIonicAngular } from '@ionic/angular';
bootstrapApplication(AppComponent, {
providers: [
...,
provideIonicAngular({
// Not recommended when your app requires reactive values
backButtonText: 'Go Back'
})
]
})
Recommended
<ion-back-button [text]="backButtonText"></ion-back-button>
@Component(...)
class MyComponent {
/**
* The back button text can be updated
* anytime the locale changes.
*/
backButtonText = 'Go Back';
}
Not recommended
import { setupIonicReact } from '@ionic/react';
setupIonicReact({
// Not recommended when your app requires reactive values
backButtonText: 'Go Back',
});
Recommended
import { useState } from 'react';
import { IonBackButton } from '@ionic/react';
const ExampleComponent = () => {
const [backButtonText, setBackButtonText] = useState('Go Back');
return (
{/*
* The back button text can be updated
* anytime the locale changes.
*/}
<IonBackButton text={backButtonText}></IonBackButton>
)
}
Not recommended
import { IonicVue } from '@ionic/vue';
import { createApp } from 'vue';
// Not recommended when your app requires reactive values
createApp(App).use(IonicVue, {
backButtonText: 'Go Back',
});
Recommended
<template>
<ion-back-button :text="backButtonText"></ion-back-button>
</template>
<script setup lang="ts">
import { IonBackButton } from '@ionic/vue';
import { ref } from 'vue';
/**
* The back button text can be updated
* anytime the locale changes.
*/
const backButtonText = ref('Go Back');
</script>
プラットフォームごとの設定
Ionic Config は、プラットフォームごとに設定することもできます。たとえば、これにより、潜在的に遅いデバイスのブラウザでアプリが実行されている場合にアニメーションを無効にできます。開発者は、Platform ユーティリティを活用してこれを実現できます。
次の例では、モバイル Web ブラウザでアプリが実行されている場合にのみ、Ionic アプリのすべてのアニメーションを無効にしています。
- Angular
- Angular (Standalone)
- React
- Vue
設定は実行時に行うため、依存性注入で Platform を取得することはできません。代わりに、provider が内部で使う関数を直接呼び出せます。
検出できるプラットフォームは Angular Platform のドキュメント を参照してください。
/*
* IonicModule is deprecated and will be removed in a future major version.
* Refer to the "Angular (Standalone)" tab to use `provideIonicAngular()` instead.
*/
import { isPlatform, IonicModule } from '@ionic/angular/lazy';
@NgModule({
...
imports: [
IonicModule.forRoot({
animated: !isPlatform('mobileweb')
})
],
...
})
設定は実行時に行うため、依存性注入で Platform を取得することはできません。代わりに、provider が内部で使う関数を直接呼び出せます。
検出できるプラットフォームは Angular Platform のドキュメント を参照してください。
import { isPlatform, provideIonicAngular } from '@ionic/angular';
bootstrapApplication(AppComponent, {
providers: [
...,
provideIonicAngular({
animated: !isPlatform('mobileweb')
})
]
})
検出できるプラットフォームは React Platform のドキュメント を参照してください。
import { isPlatform, setupIonicReact } from '@ionic/react';
setupIonicReact({
animated: !isPlatform('mobileweb'),
});
検出できるプラットフォームは Vue Platform のドキュメント を参照してください。
import { IonicVue, isPlatform } from '@ionic/vue';
createApp(App).use(IonicVue, {
animated: !isPlatform('mobileweb'),
});
フォールバック
次の例では、プラットフォームに基づいて完全に異なる設定を設定でき、プラットフォームが一致しない場合はデフォルト設定にフォールバックします:
- Angular
- Angular (Standalone)
- React
- Vue
/*
* IonicModule is deprecated and will be removed in a future major version.
* Refer to the "Angular (Standalone)" tab to use `provideIonicAngular()` instead.
*/
import { isPlatform, IonicModule } from '@ionic/angular/lazy';
const getConfig = () => {
if (isPlatform('hybrid')) {
return {
tabButtonLayout: 'label-hide'
}
}
return {
tabButtonLayout: 'icon-top'
};
}
@NgModule({
...
imports: [
IonicModule.forRoot(getConfig())
],
...
});
import { isPlatform, provideIonicAngular } from '@ionic/angular';
const getConfig = () => {
if (isPlatform('hybrid')) {
return {
tabButtonLayout: 'label-hide'
}
}
return {
tabButtonLayout: 'icon-top'
};
}
bootstrapApplication(AppComponent, {
providers: [
...,
provideIonicAngular(getConfig())
]
})
import { isPlatform, setupIonicReact } from '@ionic/react';
const getConfig = () => {
if (isPlatform('hybrid')) {
return {
tabButtonLayout: 'label-hide',
};
}
return {
tabButtonLayout: 'icon-top',
};
};
setupIonicReact(getConfig());
import { IonicVue, isPlatform } from '@ionic/vue';
const getConfig = () => {
if (isPlatform('hybrid')) {
return {
tabButtonLayout: 'label-hide',
};
}
return {
tabButtonLayout: 'icon-top',
};
};
createApp(App).use(IonicVue, getConfig());
オーバーライド
この最後の例では、異なるプラットフォーム要件に基づいて設定オブジェクトを累積できます。
- Angular
- Angular (Standalone)
- React
- Vue
/*
* IonicModule is deprecated and will be removed in a future major version.
* Refer to the "Angular (Standalone)" tab to use `provideIonicAngular()` instead.
*/
import { isPlatform, IonicModule } from '@ionic/angular/lazy';
const getConfig = () => {
let config = {
animated: false
};
if (isPlatform('iphone')) {
config = {
...config,
backButtonText: 'Previous'
}
}
return config;
}
@NgModule({
...
imports: [
IonicModule.forRoot(getConfig())
],
...
});
import { isPlatform, provideIonicAngular } from '@ionic/angular';
const getConfig = () => {
let config = {
animated: false
};
if (isPlatform('iphone')) {
config = {
...config,
backButtonText: 'Previous'
}
}
return config;
}
bootstrapApplication(AppComponent, {
providers: [
...,
provideIonicAngular(getConfig())
]
})
import { isPlatform, setupIonicReact } from '@ionic/react';
const getConfig = () => {
let config = {
animated: false,
};
if (isPlatform('iphone')) {
config = {
...config,
backButtonText: 'Previous',
};
}
return config;
};
setupIonicReact(getConfig());
import { IonicVue, isPlatform } from '@ionic/vue';
const getConfig = () => {
let config = {
animated: false,
};
if (isPlatform('iphone')) {
config = {
...config,
backButtonText: 'Previous',
};
}
return config;
};
createApp(App).use(IonicVue, getConfig());
モードへのアクセス
場合によっては、アプリケーションロジック内で現在の Ionic モードにプログラム的にアクセスする必要があるかもしれません。これは、条件付き動作を適用したり、特定のアセットを取得したり、アクティブなスタイリングモードに基づいて他のアクションを実行したりするのに役立ちます。
設定の読み取り(Angular)
Ionic Angular は、Ionic Config にアクセスするためのConfig provider を提供します。
get
| 説明 | 設定値をanyとして返します。設定が定義されていない場合はnullを返します。 |
| シグネチャ | get(key: string, fallback?: any) => any |
例
- Angular
- Angular (Standalone)
import { Config } from '@ionic/angular/lazy';
@Component(...)
class AppComponent {
constructor(config: Config) {
const mode = config.get('mode');
}
}
import { Config } from '@ionic/angular';
@Component(...)
class AppComponent {
constructor(config: Config) {
const mode = config.get('mode');
}
}
getBoolean
| 説明 | 設定値をbooleanとして返します。設定が定義されていない場合はfalseを返します。 |
| シグネチャ | getBoolean(key: string, fallback?: boolean) => boolean |
例
- Angular
- Angular (Standalone)
import { Config } from '@ionic/angular/lazy';
@Component(...)
class AppComponent {
constructor(config: Config) {
const swipeBackEnabled = config.getBoolean('swipeBackEnabled');
}
}
import { Config } from '@ionic/angular';
@Component(...)
class AppComponent {
constructor(config: Config) {
const swipeBackEnabled = config.getBoolean('swipeBackEnabled');
}
}
getNumber
| 説明 | 設定値をnumberとして返します。設定が定義されていない場合は0を返します。 |
| シグネチャ | getNumber(key: string, fallback?: number) => number |
インターフェース
IonicConfig
以下は、Ionic が使用する設定オプションです。
| Config | Type | 説明 |
|---|---|---|
actionSheetEnter | AnimationBuilder | すべての ion-action-sheet の表示時に使うカスタムアニメーションを指定し、デフォルトの「animation」を上書きします。 |
actionSheetLeave | AnimationBuilder | すべての ion-action-sheet の終了時に使うカスタムアニメーションを指定し、デフォルトの「animation」を上書きします。 |
alertEnter | AnimationBuilder | すべての ion-alert の表示時に使うカスタムアニメーションを指定し、デフォルトの「animation」を上書きします。 |
alertLeave | AnimationBuilder | すべての ion-alert の終了時に使うカスタムアニメーションを指定し、デフォルトの「animation」を上書きします。 |
animated | boolean | trueの場合、Ionic はアプリ全体ですべてのアニメーションやトランジションを有効にします。 |
backButtonDefaultHref | string | すべての<ion-back-button>コンポーネントにおけるdefaultHrefプロパティのデフォルト値を変更します。 |
backButtonIcon | string | すべての<ion-back-button>コンポーネントにおける、デフォルトのアイコンを上書きします。 |
backButtonText | string | すべての<ion-back-button>コンポーネントにおける、デフォルトのテキストを上書きします。 |
innerHTMLTemplatesEnabled | boolean | 関連するコンポーネント:ion-alert、ion-infinite-scroll-content、ion-loading、ion-refresher-content、ion-select-option、ion-toast。trueの場合、関連するコンポーネントに渡される内容は、プレーンテキストではなく、HTML として処理されます。デフォルトはfalseです。 |
hardwareBackButton | boolean | trueの場合、Ionic は Android デバイスにおけるハードウェアのバックボタンに反応します。 |
infiniteLoadingSpinner | SpinnerTypes | すべての<ion-infinite-scroll-content>コンポーネントにおいて、デフォルトのスピナータイプをオーバーライドします。 |
loadingEnter | AnimationBuilder | すべての ion-loading の表示時に使うカスタムアニメーションを指定し、デフォルトの「animation」を上書きします。 |
loadingLeave | AnimationBuilder | すべての ion-loading の終了時に使うカスタムアニメーションを指定し、デフォルトの「animation」を上書きします。 |
loadingSpinner | SpinnerTypes | すべてのion-loadingオーバーレイのデフォルトのスピナーをオーバーライドします。 |
logLevel | 'OFF' | 'ERROR' | 'WARN' | Ionic Framework のログレベルを設定します。'OFF' ではエラーも警告も記録しません。'ERROR' ではエラーのみ、'WARN' ではエラーと警告を記録します。 |
menuIcon | string | すべての<ion-menu-button>コンポーネントにおける、デフォルトのアイコンを上書きします。 |
menuType | string | すべての<ion-menu>コンポーネントのデフォルトのメニュータイプを変更します。 |
modalEnter | AnimationBuilder | すべての ion-modal の表示時に使うカスタムアニメーションを指定し、デフォルトの「animation」を上書きします。 |
modalLeave | AnimationBuilder | すべての ion-modal の終了時に使うカスタムアニメーションを指定し、デフォルトの「animation」を上書きします。 |
mode | Mode | このモードによって、アプリ全体でどのプラットフォーム向けのスタイルを使用するかが決まります。 |
navAnimation | AnimationBuilder | アプリ全体で、すべての ion-nav と ion-router-outlet のデフォルトの「animation」を上書きします。 |
platform | PlatformConfig | デフォルトのプラットフォーム検出方法を上書きします。 |
popoverEnter | AnimationBuilder | すべての ion-popover の表示時に使うカスタムアニメーションを指定し、デフォルトの「animation」を上書きします。 |
popoverLeave | AnimationBuilder | すべての ion-popover の終了時に使うカスタムアニメーションを指定し、デフォルトの「animation」を上書きします。 |
refreshingIcon | string | すべての<ion-refresh-content>コンポーネントにおける、デフォルトのアイコンを上書きします。 |
refreshingSpinner | SpinnerTypes | すべての<ion-refresh-content>コンポーネントにおいて、デフォルトのスピナータイプをオーバーライドします。 |
rippleEffect | boolean | true の場合、アプリ全体で Material Design のリップル効果を有効にします。 |
sanitizerEnabled | boolean | true の場合、カスタム HTML を受け取るコンポーネントのプロパティに対して、Ionic が基本的な DOM sanitizer を有効にします。 |
spinner | SpinnerTypes | すべての<ion-spinner>コンポーネントにおける、デフォルトのスピナーをオーバーライドします。 |
statusTap | boolean | trueの場合、ステータスバーをクリックしたりタップしたりすると、コンテンツが上部にスクロールします。 |
swipeBackEnabled | boolean | true の場合、アプリ全体で「swipe-to-go-back」ジェスチャーを有効にします。 |
tabButtonLayout | TabButtonLayout | アプリ全体で、すべての ion-bar-button のデフォルトの「layout」を上書きします。 |
toastDuration | number | すべてのion-toastコンポーネントにおける、デフォルトのduration値を上書きします。 |
toastEnter | AnimationBuilder | すべての ion-toast の表示時に使うカスタムアニメーションを指定し、デフォルトの「animation」を上書きします。 |
toastLeave | AnimationBuilder | すべての ion-toast の終了時に使うカスタムアニメーションを指定し、デフォルトの「animation」を上書きします。 |
toggleOnOffLabels | boolean | すべてのion-toggleコンポーネントにおける、デフォルトのenableOnOffLabels設定を上書きします。 |
experimentalCloseWatcher | boolean | Experimental: true の場合、メニューやオーバーレイを閉じる操作やナビゲーションで、Escape キーとハードウェアの戻るボタンの入力をすべて CloseWatcher API で処理します。hardwareBackButton オプションも true に設定してください。 |
focusManagerPriority | FocusManagerPriority[] | Experimental: 設定すると、ページ遷移後に Ionic が適切な要素にフォーカスを移します。これにより、支援技術を使うユーザーにもページの切り替わりが伝わります。デフォルトでは無効です。 |