Skip to content

Commit 2fc5f72

Browse files
committed
details for application api
1 parent 74ff0ad commit 2fc5f72

File tree

1 file changed

+103
-57
lines changed

1 file changed

+103
-57
lines changed

src/api/application.md

Lines changed: 103 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22

33
## createApp()
44

5-
Creates an application instance. Expects the root component as the first argument, and optional props to be passed to the root component as the second argument.
5+
Creates an application instance.
66

77
- **Type**
88

99
```ts
1010
function createApp(rootComponent: Component, rootProps?: object): App
1111
```
1212

13+
- **Details**
14+
15+
The first argument is the root component. The second optional argument is the props to be passed to the root component.
16+
1317
- **Example**
1418

1519
With inline root component:
@@ -39,13 +43,7 @@ Creates an application instance in [SSR Hydration](/guide/scaling-up/ssr.html#cl
3943

4044
## app.mount()
4145

42-
Mounts the application instance in a container element. The first argument can either be a CSS selector (the first matched element will be used) or an actual DOM element. Returns the root component instance.
43-
44-
If the component has a template or a render function defined, it will replace any existing DOM nodes inside the container. Otherwise, if the runtime compiler is available, the `innerHTML` of the container will be used as the template.
45-
46-
In SSR hydration mode, it will hydrate the existing DOM nodes inside the container. If there are [mismatches](/guide/scaling-up/ssr.html#hydration-mismatch), the existing DOM nodes will be morphed to match the expected output.
47-
48-
For each app instance, `mount()` can only be called once.
46+
Mounts the application instance in a container element.
4947

5048
- **Type**
5149

@@ -55,6 +53,16 @@ For each app instance, `mount()` can only be called once.
5553
}
5654
```
5755

56+
- **Details**
57+
58+
The first argument can either be a CSS selector (the first matched element will be used) or an actual DOM element. Returns the root component instance.
59+
60+
If the component has a template or a render function defined, it will replace any existing DOM nodes inside the container. Otherwise, if the runtime compiler is available, the `innerHTML` of the container will be used as the template.
61+
62+
In SSR hydration mode, it will hydrate the existing DOM nodes inside the container. If there are [mismatches](/guide/scaling-up/ssr.html#hydration-mismatch), the existing DOM nodes will be morphed to match the expected output.
63+
64+
For each app instance, `mount()` can only be called once.
65+
5866
- **Example**
5967

6068
```js
@@ -72,7 +80,7 @@ For each app instance, `mount()` can only be called once.
7280

7381
## app.unmount()
7482

75-
Unmounts a mounted application instance. This will in turn trigger the unmount lifecycle hooks for all components in the application's component tree.
83+
Unmounts a mounted application instance, triggering the unmount lifecycle hooks for all components in the application's component tree.
7684

7785
- **Type**
7886

@@ -84,7 +92,7 @@ Unmounts a mounted application instance. This will in turn trigger the unmount l
8492

8593
## app.provide()
8694

87-
Provide a value that can be injected in all descendent components within the application. Expects the injection key as the first argument, and the provided value as the second. Returns the application instance itself.
95+
Provide a value that can be injected in all descendent components within the application.
8896

8997
- **Type**
9098

@@ -94,6 +102,10 @@ Provide a value that can be injected in all descendent components within the app
94102
}
95103
```
96104

105+
- **Details**
106+
107+
Expects the injection key as the first argument, and the provided value as the second. Returns the application instance itself.
108+
97109
- **Example**
98110

99111
```js
@@ -207,11 +219,7 @@ Registers a global custom directive if passing both a name string and a directiv
207219

208220
## app.use()
209221

210-
Installs a [plugin](/guide/reusability/plugins.html). Expects the plugin as the first argument, and optional plugin options as the second argument.
211-
212-
The plugin can either be an object with an `install()` method, or a directly a function (which itself will used as the install method). The options (second argument of `app.use()`) will be passed along to the plugin's install method.
213-
214-
When `app.use()` is called on the same plugin multiple times, the plugin will be installed only once.
222+
Installs a [plugin](/guide/reusability/plugins.html).
215223

216224
- **Type**
217225

@@ -221,6 +229,14 @@ When `app.use()` is called on the same plugin multiple times, the plugin will be
221229
}
222230
```
223231

232+
- **Details**
233+
234+
Expects the plugin as the first argument, and optional plugin options as the second argument.
235+
236+
The plugin can either be an object with an `install()` method, or a directly a function (which itself will used as the install method). The options (second argument of `app.use()`) will be passed along to the plugin's install method.
237+
238+
When `app.use()` is called on the same plugin multiple times, the plugin will be installed only once.
239+
224240
- **Example**
225241

226242
```js
@@ -240,6 +256,12 @@ When `app.use()` is called on the same plugin multiple times, the plugin will be
240256

241257
Applies a global mixin (scoped to the application). A global mixin applies its included options to every component instance in the application.
242258

259+
:::warning Not Recommended
260+
Mixins are supported in Vue 3 mainly for backwards compatibility due to its wide-spread use in ecosystem libraries. Use of mixins, especially global mixins, should be avoided in application code.
261+
262+
For logic reuse, prefer [Composables](/guide/reusability/composables.html) instead.
263+
:::
264+
243265
- **Type**
244266

245267
```ts
@@ -248,12 +270,6 @@ Applies a global mixin (scoped to the application). A global mixin applies its i
248270
}
249271
```
250272

251-
:::warning Not Recommended
252-
Mixins are supported in Vue 3 mainly for backwards compatibility due to its wide-spread use in ecosystem libraries. Use of mixins, especially global mixins, should be avoided in application code.
253-
254-
For logic reuse, prefer [Composables](/guide/reusability/composables.html) instead.
255-
:::
256-
257273
## app.version
258274

259275
Provides the version of Vue that the application was created with. This is useful inside [plugins](/guide/reusability/plugins.html), where you might need conditional logic based on different Vue versions.
@@ -297,17 +313,7 @@ console.log(app.config)
297313

298314
## app.config.errorHandler
299315

300-
Assign a global handler for uncaught errors from the following sources:
301-
302-
- Component renders
303-
- Event handlers
304-
- Lifecycle hooks
305-
- `setup()` function
306-
- Watchers
307-
- Custom directive hooks
308-
- Transition hooks
309-
310-
The error handler receives three arguments: the error, the component instance that triggered the error, and an information string specifying the error source type.
316+
Assign a global handler for uncaught errors propagating from within the application.
311317

312318
- **Type**
313319

@@ -323,6 +329,20 @@ The error handler receives three arguments: the error, the component instance th
323329
}
324330
```
325331

332+
- **Details**
333+
334+
The error handler receives three arguments: the error, the component instance that triggered the error, and an information string specifying the error source type.
335+
336+
It can capture errors from the following sources:
337+
338+
- Component renders
339+
- Event handlers
340+
- Lifecycle hooks
341+
- `setup()` function
342+
- Watchers
343+
- Custom directive hooks
344+
- Transition hooks
345+
326346
- **Example**
327347

328348
```js
@@ -333,13 +353,7 @@ The error handler receives three arguments: the error, the component instance th
333353

334354
## app.config.warnHandler
335355

336-
Assign a custom handler for runtime warnings from Vue. It receives the warning message as the first argument, the source component instance as the second argument, and a component trace string as the third.
337-
338-
This can be used to filter out specific warnings to reduce console verbosity. All Vue warnings should be addressed during development, so this is only recommended during debug sessions to focus on specific warnings among many, and should be removed once the debugging is done.
339-
340-
:::tip
341-
Warnings only work during development, so this config is ignored in production mode.
342-
:::
356+
Assign a custom handler for runtime warnings from Vue.
343357

344358
- **Type**
345359

@@ -353,6 +367,16 @@ Warnings only work during development, so this config is ignored in production m
353367
}
354368
```
355369

370+
- **Details**
371+
372+
The warning handler receives the warning message as the first argument, the source component instance as the second argument, and a component trace string as the third.
373+
374+
It can be used to filter out specific warnings to reduce console verbosity. All Vue warnings should be addressed during development, so this is only recommended during debug sessions to focus on specific warnings among many, and should be removed once the debugging is done.
375+
376+
:::tip
377+
Warnings only work during development, so this config is ignored in production mode.
378+
:::
379+
356380
- **Example**
357381

358382
```js
@@ -383,12 +407,16 @@ This config option is only respected when using the full build (i.e. the standal
383407
384408
### app.compilerOptions.isCustomElement
385409
386-
Specifies a check method to recognize native custom elements. If a tag name matches this condition, Vue will render it as a native element instead of attempting to resolve it as a Vue component.
387-
388-
Native HTML and SVG tags don't need to be matched in this function - Vue's parser recognizes them automatically.
410+
Specifies a check method to recognize native custom elements.
389411
390412
- **Type:** `(tag: string) => boolean`
391413
414+
- **Details**
415+
416+
Should return `true` if the tag should be treated as a native custom element. For a matched tag, Vue will render it as a native element instead of attempting to resolve it as a Vue component.
417+
418+
Native HTML and SVG tags don't need to be matched in this function - Vue's parser recognizes them automatically.
419+
392420
- **Example**
393421
394422
```js
@@ -404,17 +432,19 @@ Native HTML and SVG tags don't need to be matched in this function - Vue's parse
404432
405433
Adjusts template whitespace handling behavior.
406434
407-
Vue removes / condenses whitespaces in templates to produce more efficient compiled output. The default strategy is "condense", with the following behavior:
435+
- **Type:** `'condense' | 'preserve'`
408436
409-
1. Leading / ending whitespaces inside an element are condensed into a single space.
410-
2. Whitespaces between elements that contain newlines are removed.
411-
3. Consecutive whitespaces in text nodes are condensed into a single space.
437+
- **Default:** `'condense'`
412438
413-
Setting this option to `'preserve'` will disable (2) and (3).
439+
- **Details**
414440
415-
- **Type:** `'condense' | 'preserve'`
441+
Vue removes / condenses whitespaces in templates to produce more efficient compiled output. The default strategy is "condense", with the following behavior:
416442
417-
- **Default:** `'condense'`
443+
1. Leading / ending whitespaces inside an element are condensed into a single space.
444+
2. Whitespaces between elements that contain newlines are removed.
445+
3. Consecutive whitespaces in text nodes are condensed into a single space.
446+
447+
Setting this option to `'preserve'` will disable (2) and (3).
418448
419449
- **Example**
420450
@@ -424,12 +454,16 @@ Setting this option to `'preserve'` will disable (2) and (3).
424454
425455
### app.compilerOptions.delimiters
426456
427-
Adjusts the delimiters used for text interpolation within the template. This is typically used to avoid conflicting with server-side frameworks that also use mustache syntax.
457+
Adjusts the delimiters used for text interpolation within the template.
428458
429459
- **Type:** `[string, string]`
430460
431461
- **Default:** `{{ "['\u007b\u007b', '\u007d\u007d']" }}`
432462
463+
- **Details**
464+
465+
This is typically used to avoid conflicting with server-side frameworks that also use mustache syntax.
466+
433467
- **Example**
434468
435469
```js
@@ -441,12 +475,14 @@ Adjusts the delimiters used for text interpolation within the template. This is
441475
442476
Adjusts treatment of HTML comments in templates.
443477
444-
By default, Vue will remove the comments in production. Setting this option to `true` will force Vue to preserve comments even in production. Comments are always preserved during development. This option is typically used when Vue is used with other libraries that rely on HTML comments.
445-
446478
- **Type:** `boolean`
447479
448480
- **Default:** `false`
449481
482+
- **Details**
483+
484+
By default, Vue will remove the comments in production. Setting this option to `true` will force Vue to preserve comments even in production. Comments are always preserved during development. This option is typically used when Vue is used with other libraries that rely on HTML comments.
485+
450486
- **Example**
451487
452488
```js
@@ -455,9 +491,7 @@ By default, Vue will remove the comments in production. Setting this option to `
455491
456492
## app.config.globalProperties
457493
458-
An object that can be used to register global properties that can be accessed on any component instance inside the application. This is a replacement of Vue 2's `Vue.prototype` which is no longer present in Vue 3. As with anything global, this should be used sparingly.
459-
460-
If a global property conflicts with a component’s own property, the component's own property will have higher priority.
494+
An object that can be used to register global properties that can be accessed on any component instance inside the application.
461495
462496
- **Type**
463497
@@ -467,6 +501,12 @@ If a global property conflicts with a component’s own property, the component'
467501
}
468502
```
469503
504+
- **Details**
505+
506+
This is a replacement of Vue 2's `Vue.prototype` which is no longer present in Vue 3. As with anything global, this should be used sparingly.
507+
508+
If a global property conflicts with a component’s own property, the component's own property will have higher priority.
509+
470510
- **Usage**
471511
472512
```js
@@ -487,8 +527,6 @@ If a global property conflicts with a component’s own property, the component'
487527
488528
An object for defining merging strategies for custom component options.
489529
490-
The merge strategy receives the value of that option defined on the parent and child instances as the first and second arguments, respectively.
491-
492530
- **Type**
493531
494532
```ts
@@ -499,6 +537,14 @@ The merge strategy receives the value of that option defined on the parent and c
499537
type OptionMergeFunction = (to: unknown, from: unknown) => any
500538
```
501539
540+
- **Details**
541+
542+
Some plugins / libraries add support for custom component options (by injecting global mixins). These options may require special merging logic when the same option needs to be "merged" from multiple sources (e.g. mixins or component inheritance).
543+
544+
A merge strategy function can registered for a custom option by assigning it on the `app.config.optionMergeStrategies` object using the option's name as the key.
545+
546+
The merge strategy function receives the value of that option defined on the parent and child instances as the first and second arguments, respectively.
547+
502548
- **Example**
503549
504550
```js

0 commit comments

Comments
 (0)