|
2 | 2 | title: Config |
3 | 3 | --- |
4 | 4 |
|
5 | | -<head> |
6 | | - <title>Config | Ionic Config to Change Component Properties Globally</title> |
7 | | - <meta |
8 | | - name="description" |
9 | | - content="Ionic Config provides a way to change the properties of components globally across an app. Config can set the app mode, tab button layout, animations, and more." |
10 | | - /> |
11 | | -</head> |
12 | | - |
13 | 5 | Ionic Config provides a way to change the properties of components globally across an app. It can set the app mode, tab button layout, animations, and more. |
14 | 6 |
|
15 | 7 | ## Global Config |
16 | 8 |
|
17 | | -To override the default Ionic configurations for your app, provide your own custom config to `IonicModule.forRoot(...)`. The available config keys can be found in the [`IonicConfig`](#ionicconfig) interface. |
| 9 | +The available config keys can be found in the [`IonicConfig`](#ionicconfig) interface. |
18 | 10 |
|
19 | | -For example, to disable ripple effects and default the mode to Material Design: |
| 11 | +The following example disables ripple effects and default the mode to Material Design: |
20 | 12 |
|
21 | | -```tsx title="app.module.ts" |
22 | | -import { IonicModule } from '@ionic/angular'; |
| 13 | +import GlobalExample from '@site/docs/developing/config/global/index.md'; |
23 | 14 |
|
24 | | -@NgModule({ |
25 | | - ... |
26 | | - imports: [ |
27 | | - IonicModule.forRoot({ |
28 | | - rippleEffect: false, |
29 | | - mode: 'md' |
30 | | - }) |
31 | | - ], |
32 | | - ... |
33 | | -}) |
34 | | -``` |
| 15 | +<GlobalExample /> |
35 | 16 |
|
36 | 17 | ## Per-Component Config |
37 | 18 |
|
38 | 19 | Ionic Config is not reactive. Updating the config's value after the component has rendered will result in the previous value. It is recommended to use a component's properties instead of updating the config, when you require reactive values. |
39 | 20 |
|
40 | | -**Not recommended** |
41 | | - |
42 | | -```ts |
43 | | -import { IonicModule } from '@ionic/angular'; |
44 | | - |
45 | | -@NgModule({ |
46 | | - ... |
47 | | - imports: [ |
48 | | - IonicModule.forRoot({ |
49 | | - // Not recommended when your app requires reactive values |
50 | | - backButtonText: 'Go Back' |
51 | | - }) |
52 | | - ], |
53 | | - ... |
54 | | -}) |
55 | | -``` |
56 | | - |
57 | | -**Recommended** |
58 | | - |
59 | | -```html |
60 | | -<ion-back-button [text]="backButtonText"></ion-back-button> |
61 | | -``` |
62 | | - |
63 | | -```ts |
64 | | -@Component(...) |
65 | | -class MyComponent { |
66 | | - backButtonText = this.config.get('backButtonText'); |
67 | | - |
68 | | - constructor(private config: Config) { } |
| 21 | +import PerComponentExample from '@site/docs/developing/config/per-component/index.md'; |
69 | 22 |
|
70 | | - localeChanged(locale: string) { |
71 | | - if (locale === 'es_ES') { |
72 | | - this.backButtonText = 'Devolver'; |
73 | | - } |
74 | | - } |
75 | | - |
76 | | -} |
77 | | -``` |
| 23 | +<PerComponentExample /> |
| 24 | + |
78 | 25 |
|
79 | 26 | ## Per-Platform Config |
80 | 27 |
|
81 | 28 | Ionic Config can also be set on a per-platform basis. For example, this allows you to disable animations if the app is being run in a browser on a potentially slower device. Developers can take advantage of the Platform utilities to accomplish this. |
82 | 29 |
|
83 | | -Since the config is set at runtime, you will not have access to the Platform Dependency Injection. Instead, you can use the underlying functions that the provider uses directly. |
84 | | - |
85 | 30 | In the following example, we are disabling all animations in our Ionic app only if the app is running in a mobile web browser. |
86 | | -The `isPlatform()` call returns `true` or `false` based upon the platform that is passed in. See the [Platform Documentation](platform.md#platforms) for a list of possible values. |
87 | | - |
88 | | -```tsx |
89 | | -import { isPlatform, IonicModule } from '@ionic/angular'; |
90 | | - |
91 | | -@NgModule({ |
92 | | - ... |
93 | | - imports: [ |
94 | | - IonicModule.forRoot({ |
95 | | - animated: !isPlatform('mobileweb') |
96 | | - }) |
97 | | - ], |
98 | | - ... |
99 | | -}) |
100 | | -``` |
101 | 31 |
|
102 | | -**Per-platform config with fallback for unmatched platforms:** |
| 32 | +import PerPlatformExample from '@site/docs/developing/config/per-platform/index.md'; |
103 | 33 |
|
104 | | -```tsx |
105 | | -import { isPlatform, IonicModule } from '@ionic/angular'; |
| 34 | +<PerPlatformExample /> |
106 | 35 |
|
107 | | -const getConfig = () => { |
108 | | - if (isPlatform('hybrid')) { |
109 | | - return { |
110 | | - backButtonText: 'Previous', |
111 | | - tabButtonLayout: 'label-hide' |
112 | | - } |
113 | | - } |
114 | 36 |
|
115 | | - return { |
116 | | - menuIcon: 'ellipsis-vertical' |
117 | | - } |
118 | | -} |
119 | | -@NgModule({ |
120 | | - ... |
121 | | - imports: [ |
122 | | - IonicModule.forRoot(getConfig()) |
123 | | - ], |
124 | | - ... |
125 | | -}) |
126 | | -``` |
| 37 | +### Fallbacks |
127 | 38 |
|
128 | | -**Per-platform config overrides:** |
| 39 | +The next example allows you to set an entirely different configuration based upon the platform, falling back to a default config if no platforms match: |
129 | 40 |
|
130 | | -```tsx |
131 | | -import { isPlatform, IonicModule } from '@ionic/angular'; |
| 41 | +import PerPlatformFallbackExample from '@site/docs/developing/config/per-platform-overrides/index.md'; |
132 | 42 |
|
133 | | -const getConfig = () => { |
134 | | - let config = { |
135 | | - animated: false |
136 | | - }; |
| 43 | +<PerPlatformFallbackExample /> |
137 | 44 |
|
138 | | - if (isPlatform('iphone')) { |
139 | | - config = { |
140 | | - ...config, |
141 | | - backButtonText: 'Previous' |
142 | | - } |
143 | | - } |
| 45 | +### Overrides |
144 | 46 |
|
145 | | - return config; |
146 | | -} |
147 | | -@NgModule({ |
148 | | - ... |
149 | | - imports: [ |
150 | | - IonicModule.forRoot(getConfig()) |
151 | | - ], |
152 | | - ... |
153 | | -}) |
154 | | -``` |
| 47 | +This final example allows you to accumulate a config object based upon different platform requirements. |
155 | 48 |
|
156 | | -## Methods |
| 49 | +import PerPlatformOverridesExample from '@site/docs/developing/config/per-platform-fallback/index.md'; |
| 50 | + |
| 51 | +<PerPlatformOverridesExample /> |
| 52 | + |
| 53 | +## Reading the Config (Angular) |
| 54 | + |
| 55 | +Ionic Angular provides a `Config` provider for accessing the Ionic Config. |
157 | 56 |
|
158 | 57 | ### get |
159 | 58 |
|
@@ -202,19 +101,6 @@ class AppComponent { |
202 | 101 | | **Description** | Returns a config value as a `number`. Returns `0` if the config is not defined. | |
203 | 102 | | **Signature** | `getNumber(key: string, fallback?: number) => number` | |
204 | 103 |
|
205 | | -#### Examples |
206 | | - |
207 | | -```ts |
208 | | -import { Config } from '@ionic/angular'; |
209 | | - |
210 | | -@Component(...) |
211 | | -class AppComponent { |
212 | | - constructor(config: Config) { |
213 | | - const keyboardHeight = config.getNumber('keyboardHeight'); |
214 | | - } |
215 | | -} |
216 | | -``` |
217 | | - |
218 | 104 | ## Interfaces |
219 | 105 |
|
220 | 106 | ### IonicConfig |
@@ -254,5 +140,7 @@ Below are the config options that Ionic uses. |
254 | 140 | | `statusTap` | `boolean` | If `true`, clicking or tapping the status bar will cause the content to scroll to the top. | |
255 | 141 | | `swipeBackEnabled` | `boolean` | If `true`, Ionic will enable the "swipe-to-go-back" gesture across the application. | |
256 | 142 | | `tabButtonLayout` | `TabButtonLayout` | Overrides the default "layout" of all `ion-bar-button` across the whole application. | |
| 143 | +| `toastDuration` | `number` | Overrides the default `duration` for all `ion-toast` components. | |
257 | 144 | | `toastEnter` | `AnimationBuilder` | Provides a custom enter animation for all `ion-toast`, overriding the default "animation". | |
258 | 145 | | `toastLeave` | `AnimationBuilder` | Provides a custom leave animation for all `ion-toast`, overriding the default "animation". | |
| 146 | +| `toggleOnOffLabels` | `boolean` | Overrides the default `enableOnOffLabels` in all `ion-toggle` components. | |
0 commit comments