配置
Ionic Config 提供了一种跨应用全局更改组件属性的方法。它可以设置应用模式、标签按钮布局、动画等。
全局配置
可用的配置键可以在 IonicConfig 接口中找到。
以下示例禁用了波纹效果,并将模式默认设置为 Material Design:
- JavaScript
- Angular
- Angular (Standalone)
- React
- Vue
window.Ionic = {
config: {
rippleEffect: false,
mode: 'md',
},
};
import { IonicModule } from '@ionic/angular';
@NgModule({
...
imports: [
IonicModule.forRoot({
rippleEffect: false,
mode: 'md'
})
],
...
})
import { provideIonicAngular } from '@ionic/angular/standalone';
bootstrapApplication(AppComponent, {
providers: [
...,
provideIonicAngular({
rippleEffect: false,
mode: 'md'
})
]
})
必须在渲染任何 Ionic 组件(包括 IonApp)之前调用 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 不是响应式的。在组件渲染后更新配置值将导致保留先前的值。建议在需要响应式值时使用组件的属性,而不是更新配置。
- JavaScript
- Angular
- Angular (Standalone)
- React
- Vue
不推荐
window.Ionic = {
config: {
// Not recommended when your app requires reactive values
backButtonText: 'Go Back',
},
};
推荐
<ion-back-button></ion-back-button>
<script>
const backButton = document.querySelector('ion-back-button');
/**
* 返回按钮文本可以在语言环境变化时随时更新。
*/
backButton.text = 'Go Back';
</script>
不推荐
import { IonicModule } from '@ionic/angular';
@NgModule({
...
imports: [
IonicModule.forRoot({
// 当您的应用需要响应式值时,不推荐这样做
backButtonText: 'Go Back'
})
],
...
});
推荐
<ion-back-button [text]="backButtonText"></ion-back-button>
@Component(...)
class MyComponent {
/**
* 返回按钮文本可以在语言环境变化时随时更新。
*/
backButtonText = 'Go Back';
}
不推荐
import { provideIonicAngular } from '@ionic/angular/standalone';
bootstrapApplication(AppComponent, {
providers: [
...,
provideIonicAngular({
// 当您的应用需要响应式值时,不推荐这样做
backButtonText: 'Go Back'
})
]
})
推荐
<ion-back-button [text]="backButtonText"></ion-back-button>
@Component(...)
class MyComponent {
/**
* 返回按钮文本可以在语言环境变化时随时更新。
*/
backButtonText = 'Go Back';
}
不推荐
import { setupIonicReact } from '@ionic/react';
setupIonicReact({
// Not recommended when your app requires reactive values
backButtonText: 'Go Back',
});
推荐
import { useState } from 'react';
import { IonBackButton } from '@ionic/react';
const ExampleComponent = () => {
const [backButtonText, setBackButtonText] = useState('Go Back');
return (
{/*
* 返回按钮文本可以在语言环境变化时随时更新。
*/}
<IonBackButton text={backButtonText}></IonBackButton>
)
}
不推荐
import { IonicVue } from '@ionic/vue';
import { createApp } from 'vue';
// Not recommended when your app requires reactive values
createApp(App).use(IonicVue, {
backButtonText: 'Go Back',
});
推荐
<template>
<ion-back-button [text]="backButtonText"></ion-back-button>
</template>
<script setup lang="ts">
import { IonBackButton } from '@ionic/vue';
import { ref } from 'vue';
/**
* 返回按钮文本可以在语言环境变化时随时更新。
*/
const backButtonText = ref('Go Back');
</script>
按平台配置
Ionic Config 也可以按平台进行设置。例如,这允许在应用运行于可能性能较慢的浏览器中时禁用动画。开发者可以利用 Platform 工具来实现这一点。
在以下示例中,只有当应用在移动 Web 浏览器中运行时,我们才会禁用 Ionic 应用中的所有动画。
- Angular
- Angular (Standalone)
- React
- Vue
由于配置是在运行时设置的,您将无法访问平台依赖注入(Platform Dependency Injection)。不过,您可以直接使用提供者所依赖的底层函数。
有关可检测的平台类型,请参阅 Angular 平台文档。
import { isPlatform, IonicModule } from '@ionic/angular';
@NgModule({
...
imports: [
IonicModule.forRoot({
animated: !isPlatform('mobileweb')
})
],
...
})
由于配置是在运行时设置的,您将无法访问平台依赖注入(Platform Dependency Injection)。不过,您可以直接使用提供者所依赖的底层函数。
有关可检测的平台类型,请参阅 Angular 平台文档。
import { isPlatform, provideIonicAngular } from '@ionic/angular/standalone';
bootstrapApplication(AppComponent, {
providers: [
...,
provideIonicAngular({
animated: !isPlatform('mobileweb')
})
]
})
有关可检测的平台类型,请参阅 React 平台文档。
import { isPlatform, setupIonicReact } from '@ionic/react';
setupIonicReact({
animated: !isPlatform('mobileweb'),
});
有关可检测的平台类型,请参阅 Vue 平台文档。
import { IonicVue, isPlatform } from '@ionic/vue';
createApp(App).use(IonicVue, {
animated: !isPlatform('mobileweb'),
});
回退
下一个示例允许您根据平台设置完全不同的配置,如果没有匹配的平台,则回退到默认配置:
- Angular
- Angular (Standalone)
- React
- Vue
import { isPlatform, IonicModule } from '@ionic/angular';
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/standalone';
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());
覆盖
最后一个示例允许您根据不同的平台需求累积配置对象。
- Angular
- Angular (Standalone)
- React
- Vue
import { isPlatform, IonicModule } from '@ionic/angular';
const getConfig = () => {
if (isPlatform('hybrid')) {
return {
tabButtonLayout: 'label-hide'
}
}
return {
tabButtonLayout: 'icon-top'
};
}
@NgModule({
...
imports: [
IonicModule.forRoot(getConfig())
],
...
});
import { isPlatform, provideIonicAngular } from '@ionic/angular/standalone';
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());
访问模式
在某些情况下,您可能需要在应用逻辑中以编程方式访问当前的 Ionic 模式。这对于应用条件行为、获取特定资源或根据活动样式模式执行其他操作非常有用。
读取配置(Angular)
Ionic Angular 提供了一个 Config 提供者用于访问 Ionic Config。
get
| 描述 | 返回一个配置值,类型为 any。如果配置未定义,返回 null。 |
| 签名 | get(key: string, fallback?: any) => any |
示例
- Angular
- Angular(独立版)
import { Config } from '@ionic/angular';
@Component(...)
class AppComponent {
constructor(config: Config) {
const mode = config.get('mode');
}
}
import { Config } from '@ionic/angular/standalone';
@Component(...)
class AppComponent {
constructor(config: Config) {
const mode = config.get('mode');
}
}
getBoolean
| 描述 | 返回一个配置值,类型为 boolean。如果配置未定义,返回 false。 |
| 签名 | getBoolean(key: string, fallback?: boolean) => boolean |
示例
- Angular
- Angular(独立版)
import { Config } from '@ionic/angular';
@Component(...)
class AppComponent {
constructor(config: Config) {
const swipeBackEnabled = config.getBoolean('swipeBackEnabled');
}
}
import { Config } from '@ionic/angular/standalone';
@Component(...)
class AppComponent {
constructor(config: Config) {
const swipeBackEnabled = config.getBoolean('swipeBackEnabled');
}
}
getNumber
| 描述 | 返回一个配置值,类型为 number。如果配置未定义,返回 0。 |
| 签名 | getNumber(key: string, fallback?: number) => number |
接口
IonicConfig
以下是 Ionic 使用的配置选项。
| 配置 | 类型 | 描述 |
|---|---|---|
actionSheetEnter | AnimationBuilder | 为所有 ion-action-sheet 提供自定义进入动画,覆盖默认"动画"。 |
actionSheetLeave | AnimationBuilder | 为所有 ion-action-sheet 提供自定义离开动画,覆盖默认"动画"。 |
alertEnter | AnimationBuilder | 为所有 ion-alert 提供自定义进入动画,覆盖默认"动画"。 |
alertLeave | AnimationBuilder | 为所有 ion-alert 提供自定义离开动画,覆盖默认"动画"。 |
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-toast。如果为 true,传递给相关组件的内容将解析为 HTML 而非纯文本。默认为 false。 |
hardwareBackButton | boolean | 如果为 true,Ionic 将响应 Android 设备上的硬件返回按钮。 |
infiniteLoadingSpinner | SpinnerTypes | 覆盖所有 <ion-infinite-scroll-content> 组件中的默认旋转器类型。 |
loadingEnter | AnimationBuilder | 为所有 ion-loading 提供自定义进入动画,覆盖默认"动画"。 |
loadingLeave | AnimationBuilder | 为所有 ion-loading 提供自定义离开动画,覆盖默认"动画"。 |
loadingSpinner | SpinnerTypes | 覆盖所有 ion-loading 覆盖层的默认旋转器。 |
menuIcon | string | 覆盖所有 <ion-menu-button> 组件中的默认图标。 |
menuType | string | 覆盖所有 <ion-menu> 组件的默认菜单类型。 |
modalEnter | AnimationBuilder | 为所有 ion-modal 提供自定义进入动画,覆盖默认"动画"。 |
modalLeave | AnimationBuilder | 为所有 ion-modal 提供自定义离开动画,覆盖默认"动画"。 |
mode | Mode | 该模式决定整个应用使用哪种平台样式。 |
navAnimation | AnimationBuilder | 覆盖整个应用中所有 ion-nav 和 ion-router-outlet 的默认"动画"。 |
pickerEnter | AnimationBuilder | 为所有 ion-picker 提供自定义进入动画,覆盖默认"动画"。 |
pickerLeave | AnimationBuilder | 为所有 ion-picker 提供自定义离开动画,覆盖默认"动画"。 |
platform | PlatformConfig | 覆盖默认的平台检测方法。 |
popoverEnter | AnimationBuilder | 为所有 ion-popover 提供自定义进入动画,覆盖默认"动画"。 |
popoverLeave | AnimationBuilder | 为所有 ion-popover 提供自定义离开动画,覆盖默认"动画"。 |
refreshingIcon | string | 覆盖所有 <ion-refresh-content> 组件中的默认图标。 |
refreshingSpinner | SpinnerTypes | 覆盖所有 <ion-refresh-content> 组件中的默认旋转器类型。 |
sanitizerEnabled | boolean | 如果为 true,Ionic 将在接受自定义 HTML 的组件属性上启用基本的 DOM 清理器。 |
spinner | SpinnerTypes | 覆盖所有 <ion-spinner> 组件中的默认旋转器。 |
statusTap | boolean | 如果为 true,点击或轻触状态栏将导致内容滚动到顶部。 |
swipeBackEnabled | boolean | 如果为 true,Ionic 将在整个应用中启用"滑动返回"手势。 |
tabButtonLayout | TabButtonLayout | 覆盖整个应用中所有 ion-bar-button 的默认"布局"。 |
toastDuration | number | 覆盖所有 ion-toast 组件的默认 duration。 |
toastEnter | AnimationBuilder | 为所有 ion-toast 提供自定义进入动画,覆盖默认"动画"。 |
toastLeave | AnimationBuilder | 为所有 ion-toast 提供自定义离开动画,覆盖默认"动画"。 |
toggleOnOffLabels | boolean | 覆盖所有 ion-toggle 组件中的默认 enableOnOffLabels。 |
experimentalCloseWatcher | boolean | 如果为 true,将使用 CloseWatcher API 处理所有 Escape 键和硬件返回按钮的按下事件,以关闭菜单和覆盖层以及进行导航。请注意,hardwareBackButton 配置选项也必须为 true。(实验性) |