跳到主要内容
版本:v7

Config

Ionic Config 提供了一种在整个应用中全局修改组件属性的方式。它可以设置应用的模式、标签按钮布局、动画效果等。

全局配置

可用的配置项可以在 IonicConfig 接口中找到。

以下示例禁用了涟漪效果,并将默认模式设置为 Material Design:

example.js
window.Ionic = {
config: {
rippleEffect: false,
mode: 'md',
},
};

按组件配置

Ionic Config 不是响应式的。在组件渲染后更新配置值将不会生效,仍然会使用之前的值。当需要响应式值时,建议使用组件的属性(properties)而非更新配置。

不推荐

window.Ionic = {
config: {
// 当应用需要响应式值时,不推荐使用此方法
backButtonText: 'Go Back',
},
};

推荐

<ion-back-button></ion-back-button>

<script>
const backButton = document.querySelector('ion-back-button');

/**
* 返回按钮的文字可以在
* 语言环境改变时随时更新。
*/
backButton.text = 'Go Back';
</script>

按平台配置

Ionic Config 也可以基于不同平台进行设置。例如,这允许你在可能性能较慢的设备上的浏览器中运行时禁用动画。开发者可以利用 Platform 工具来实现这一点。

在以下示例中,我们仅在 Ionic 应用运行于移动端网页浏览器时,才禁用所有动画。

备注

由于配置是在运行时设置的,你将无法访问平台依赖注入(Platform Dependency Injection)。不过,你可以直接使用该提供者(provider)所依赖的底层函数。

关于可检测的平台类型,请参阅 Angular 平台文档

app.module.ts
import { isPlatform, IonicModule } from '@ionic/angular';

@NgModule({
...
imports: [
IonicModule.forRoot({
animated: !isPlatform('mobileweb')
})
],
...
})

回退机制

下面的示例允许你根据平台设置完全不同的配置,如果没有匹配的平台,则回退到默认配置:

app.module.ts
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())
],
...
});

覆盖机制

最后一个示例允许你根据不同平台的需求,逐步构建一个配置对象。

app.module.ts
import { isPlatform, IonicModule } from '@ionic/angular';

const getConfig = () => {
if (isPlatform('hybrid')) {
return {
tabButtonLayout: 'label-hide'
}
}

return {
tabButtonLayout: 'icon-top'
};
}
@NgModule({
...
imports: [
IonicModule.forRoot(getConfig())
],
...
});

访问当前模式

在某些情况下,你可能需要在应用逻辑中以编程方式访问当前 Ionic 模式。这对于应用条件行为、获取特定资源或基于当前激活的样式模式执行其他操作非常有用。

读取配置 (Angular)

Ionic Angular 提供了一个 Config 提供者(provider)来访问 Ionic 配置。

get

描述将配置值作为 any 类型返回。如果配置未定义,则返回 null
签名get(key: string, fallback?: any) => any

示例

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

示例

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 使用的配置选项。

配置项类型描述
actionSheetEnterAnimationBuilder为所有 ion-action-sheet 提供自定义的进入动画,覆盖默认的 "animation"。
actionSheetLeaveAnimationBuilder为所有 ion-action-sheet 提供自定义的离开动画,覆盖默认的 "animation"。
alertEnterAnimationBuilder为所有 ion-alert 提供自定义的进入动画,覆盖默认的 "animation"。
alertLeaveAnimationBuilder为所有 ion-alert 提供自定义的离开动画,覆盖默认的 "animation"。
animatedboolean如果为 true,Ionic 将在整个应用中启用所有动画和过渡效果。
backButtonDefaultHrefstring覆盖所有 <ion-back-button> 组件中 defaultHref 属性的默认值。
backButtonIconstring覆盖所有 <ion-back-button> 组件中的默认图标。
backButtonTextstring覆盖所有 <ion-back-button> 组件中的默认文本。
innerHTMLTemplatesEnabledboolean相关组件:ion-alert, ion-infinite-scroll-content, ion-loading, ion-refresher-content, ion-toast。如果为 true,传递给相关组件的内容将被解析为 HTML 而不是纯文本。默认为 false
hardwareBackButtonboolean如果为 true,Ionic 将响应 Android 设备上的硬件返回按钮。
infiniteLoadingSpinnerSpinnerTypes覆盖所有 <ion-infinite-scroll-content> 组件中的默认加载指示器类型。
loadingEnterAnimationBuilder为所有 ion-loading 提供自定义的进入动画,覆盖默认的 "animation"。
loadingLeaveAnimationBuilder为所有 ion-loading 提供自定义的离开动画,覆盖默认的 "animation"。
loadingSpinnerSpinnerTypes覆盖所有 ion-loading 覆盖层中的默认加载指示器。
menuIconstring覆盖所有 <ion-menu-button> 组件中的默认图标。
menuTypestring覆盖所有 <ion-menu> 组件的默认菜单类型。
modalEnterAnimationBuilder为所有 ion-modal 提供自定义的进入动画,覆盖默认的 "animation"。
modalLeaveAnimationBuilder为所有 ion-modal 提供自定义的离开动画,覆盖默认的 "animation"。
modeMode该模式决定整个应用使用哪个平台的样式。
navAnimationAnimationBuilder覆盖整个应用中所有 ion-navion-router-outlet 的默认 "animation"。
pickerEnterAnimationBuilder为所有 ion-picker 提供自定义的进入动画,覆盖默认的 "animation"。
pickerLeaveAnimationBuilder为所有 ion-picker 提供自定义的离开动画,覆盖默认的 "animation"。
platformPlatformConfig覆盖默认的平台检测方法。
popoverEnterAnimationBuilder为所有 ion-popover 提供自定义的进入动画,覆盖默认的 "animation"。
popoverLeaveAnimationBuilder为所有 ion-popover 提供自定义的离开动画,覆盖默认的 "animation"。
refreshingIconstring覆盖所有 <ion-refresh-content> 组件中的默认图标。
refreshingSpinnerSpinnerTypes覆盖所有 <ion-refresh-content> 组件中的默认加载指示器类型。
sanitizerEnabledboolean如果为 true,Ionic 将在接受自定义 HTML 的组件属性上启用基础的 DOM 清理器。
spinnerSpinnerTypes覆盖所有 <ion-spinner> 组件中的默认加载指示器。
statusTapboolean如果为 true,点击或轻触状态栏将使内容滚动到顶部。
swipeBackEnabledboolean如果为 true,Ionic 将在整个应用中启用"滑动返回"手势。
tabButtonLayoutTabButtonLayout覆盖整个应用中所有 ion-bar-button 的默认 "layout"。
toastDurationnumber覆盖所有 ion-toast 组件的默认 duration
toastEnterAnimationBuilder为所有 ion-toast 提供自定义的进入动画,覆盖默认的 "animation"。
toastLeaveAnimationBuilder为所有 ion-toast 提供自定义的离开动画,覆盖默认的 "animation"。
toggleOnOffLabelsboolean覆盖所有 ion-toggle 组件中的默认 enableOnOffLabels
experimentalCloseWatcherboolean如果为 true,将使用 CloseWatcher API 来处理所有 Escape 键和硬件返回键的按下事件,用于关闭菜单和覆盖层以及导航。注意,hardwareBackButton 配置项也必须为 true。(实验性功能)