Vue 3 Global API
The Vue 3 global API is as follows:
| No. | API & Description | Example |
|---|---|---|
| 1 | createApp() |
|
| Creates a Vue application instance, typically used for client-side rendering. | const app = createApp(App) |
|
| 2 | createSSRApp() |
|
| Creates a Vue application instance for server-side rendering (SSR). | const app = createSSRApp(App) |
|
| 3 | app.mount() |
|
| Mounts a Vue application instance to the specified DOM element. | app.mount('#app') |
|
| 4 | app.unmount() |
|
| Unmounts a Vue application instance. | app.unmount() |
|
| 5 | app.onUnmount() |
|
| Registers a callback function to be called on unmount. | app.onUnmount(() => { console.log('App is being unmounted') }) |
|
| 6 | app.component() |
|
| Registers a global component so it can be used anywhere. | app.component('MyComponent', MyComponent) |
|
| 7 | app.directive() |
|
| Registers a global directive, allowing custom directives to be created. | app.directive('focus', { mounted(el) { el.focus() } }) |
|
| 8 | app.use() |
|
| Installs a plugin; plugins can provide additional functionality to the application. | app.use(SomePlugin) |
|
| 9 | app.mixin() |
|
| Registers a global mixin, providing shared data or methods to all components. | app.mixin({ methods: { globalMethod() { ... } } }) |
|
| 10 | app.provide() |
|
| Provides dependencies to the entire application, sharing data across components. | app.provide('key', 'some value') |
|
| 11 | app.runWithContext() |
|
| Runs a function within the Vue application context, allowing access to context data. | app.runWithContext(() => { console.log('Running inside app context') }) |
|
| 12 | app.version |
|
| Gets the current Vue version number. | console.log(app.version) |
|
| 13 | app.config |
|
| Gets and sets the global configuration of the application instance. | app.config.globalProperties.$myProperty = 'value' |
|
| 14 | app.config.errorHandler |
|
| Sets a global error handler, triggered when an uncaught error occurs in the application. | app.config.errorHandler = (err, vm, info) => { console.error(err) } |
|
| 15 | app.config.warnHandler |
|
| Sets a global warning handler to process warning messages in the application. | app.config.warnHandler = (msg, vm, trace) => { console.warn(msg) } |
|
| 16 | app.config.performance |
|
| Enables performance monitoring, providing rendering performance data. | app.config.performance = true |
|
| 17 | app.config.compilerOptions |
|
| Configures template compiler options, controlling template compilation behavior. | app.config.compilerOptions.delimiters = ['[[', ']]'] |
|
| 18 | app.config.globalProperties |
|
| Sets global properties accessible by all components in the application. | app.config.globalProperties.$myGlobalProperty = 'value' |
|
| 19 | app.config.optionMergeStrategies |
|
| Defines the component option merge strategy, controlling how component options are merged. | app.config.optionMergeStrategies.methods = (parent, child) => { return { ...parent, ...child } } |
|
| 20 | app.config.idPrefix |
|
| Sets the prefix for Vue instance IDs, defaulting to "vue-". | `app.config.idPrefix = 'myApp-' | |
| 21 | app.config.throwUnhandledErrorInProduction |
|
| Controls whether unhandled errors are thrown in production. | app.config.throwUnhandledErrorInProduction = false |
|
| 22 | version |
|
| Gets the current Vue version number. | console.log(Vue.version) |
|
| 23 | nextTick() |
|
| Executes a deferred callback after the DOM update is complete, for handling asynchronous DOM updates. | nextTick(() => { console.log('DOM updated') }) |
|
| 24 | defineComponent() |
|
| Used to define components, providing type inference and a more concise syntax. | export default defineComponent({ name: 'MyComponent' }) |
|
| 25 | defineAsyncComponent() |
|
| Defines an async component that is lazily loaded when needed. | const AsyncComponent = defineAsyncComponent(() => import('./AsyncComponent.vue')) |
Application Instance API
1. createApp()
-
Function: Creates a Vue application instance.
-
Usage:
createApp(rootComponent, rootProps) -
Parameters:
-
rootComponent: The root component. -
rootProps(Optional): Props passed to the root component.
rootComponent: The root component.
rootProps (Optional): Props passed to the root component.
Example
import
{
createApp
}
from
'vue'
;
import
App from
'./App.vue'
;
const
app
=
createApp
(
App
)
;
2. createSSRApp()
-
Function: Creates a Vue application instance that supports server-side rendering (SSR).
-
Usage:
createSSRApp(rootComponent, rootProps) -
Parameters:
-
rootComponent: The root component. -
rootProps(Optional): Props passed to the root component.
rootComponent: The root component.
rootProps (Optional): Props passed to the root component.
Example
import
{
createSSRApp
}
from
'vue'
;
import
App from
'./App.vue'
;
const
app
=
createSSRApp
(
App
)
;
3. app.mount()
-
Function: Mounts the application to a DOM element.
-
Usage:
app.mount(container) -
Parameters:
-
container: A DOM element or selector string.
container: A DOM element or selector string.
Example
app.
mount
(
'#app'
)
;
4. app.unmount()
-
Function: Unmounts the application instance.
-
Usage:
app.unmount()
Example
app.
unmount
(
)
;
5. app.onUnmount()
-
Function: Registers a callback function called when the application is unmounted.
-
Usage:
app.onUnmount(callback) -
Parameters:
-
callback: The function called on unmount.
callback: The function called on unmount.
Example
app.
onUnmount
(
(
)
=>
{
console.
log
(
'App unmounted'
)
;
}
)
;
6. app.component()
-
Function: Registers or retrieves a global component.
-
Usage:
-
Register:
app.component(name, component) -
Get:
app.component(name)
Register: app.component(name, component)
Get: app.component(name)
-
Parameters:
-
name: The component name. -
component: The component definition.
name: The component name.
component: The component definition.
Example
app.
component
(
'MyComponent'
,
{
template
:
'
}
)
;
7. app.directive()
-
Function: Registers or retrieves a global directive.
-
Usage:
-
Register:
app.directive(name, directive) -
Get:
app.directive(name)
Register: app.directive(name, directive)
Get: app.directive(name)
-
Parameters:
-
name: The directive name. -
directive: The directive definition.
name: The directive name.
directive: The directive definition.
Example
app.
directive
(
'focus'
,
{
mounted
(
el
)
{
el.
focus
(
)
;
}
}
)
;
8. app.use()
-
Function: Installs a plugin.
-
Usage:
app.use(plugin, options) -
Parameters:
-
plugin: The plugin. -
options(Optional): Plugin options.
plugin: The plugin.
options (Optional): Plugin options.
Example
import
MyPlugin from
'./MyPlugin'
;
app.
use
(
MyPlugin
)
;
9. app.mixin()
-
Function: Global mixin options.
-
Usage:
app.mixin(mixin) -
Parameters:
-
mixin: The mixin object.
mixin: The mixin object.
Example
app.
mixin
(
{
created
(
)
{
console.
log
(
'Global mixin created'
)
;
}
}
)
;
10. app.provide()
-
Function: Provides global dependencies.
-
Usage:
app.provide(key, value) -
Parameters:
-
key: The dependency name. -
value: The dependency value.
key: The dependency name.
value: The dependency value.
Example
app.
provide
(
'theme'
,
'dark'
)
;
11. app.runWithContext()
-
Function: Runs a function in the current application context.
-
Usage:
app.runWithContext(fn) -
Parameters:
-
fn: The function to run.
fn: The function to run.
Example
app.
runWithContext
(
(
)
=>
{
console.
log
(
'Running with app context'
)
;
}
)
;
12. app.version
-
Function: Gets the Vue version.
-
Usage:
app.version
Example
console.
log
(
app.
version
)
;
13. app.config
-
Function: The application configuration object.
-
Usage:
app.config
Example
app.
config
.
errorHandler
=
(
err
)
=>
{
console.
error
(
err
)
;
}
;
14. app.config.errorHandler
-
Function: The global error handler function.
-
Usage:
app.config.errorHandler = (err, vm, info) => {} -
Parameters:
-
err: The error object. -
vm: The component instance where the error occurred. -
info: The error message.
err: The error object.
vm: The component instance where the error occurred.
info: The error message.
Example
app.
config
.
errorHandler
=
(
err
)
=>
{
console.
error
(
'Global error:'
,
err
)
;
}
;
15. app.config.warnHandler
-
Function: The global warning handler function.
-
Usage:
app.config.warnHandler = (msg, vm, trace) => {} -
Parameters:
-
msg: The warning message. -
vm: The component instance where the warning occurred. -
trace: The warning stack.
msg: The warning message.
vm: The component instance where the warning occurred.
trace: The warning stack.
Example
app.
config
.
warnHandler
=
(
msg
)
=>
{
console.
warn
(
'Global warning:'
,
msg
)
;
}
;
16. app.config.performance
-
Function: Enables performance tracing.
-
Usage:
app.config.performance = true
Example
app.
config
.
performance
=
true
;
17. app.config.compilerOptions
-
Function: Configures template compiler options.
-
Usage:
app.config.compilerOptions = { /* options */ }
Example
app.
config
.
compilerOptions
=
{
whitespace
:
'preserve'
}
;
18. app.config.globalProperties
-
Function: Adds a global property.
-
Usage:
app.config.globalProperties[key] = value
Example
app.
config
.
globalProperties
.$myGlobal
=
'Hello'
;
19. app.config.optionMergeStrategies
-
Function: Custom option merge strategy.
-
Usage:
app.config.optionMergeStrategies[key] = strategy
Example
app.
config
.
optionMergeStrategies
.
customOption
=
(
parent
,
child
)
=>
{
return
child
||
parent
;
}
;
20. app.config.idPrefix
-
Function: Sets the component ID prefix.
-
Usage:
app.config.idPrefix = 'my-app-'
Example
app.
config
.
idPrefix
=
'my-app-'
;
21. app.config.throwUnhandledErrorInProduction
-
Function: Throws unhandled errors in production.
-
Usage:
app.config.throwUnhandledErrorInProduction = true
Example
app.
config
.
throwUnhandledErrorInProduction
=
true
;
General API
1. version
-
Function: Gets the Vue version.
-
Usage:
Vue.version
Example
console.
log
(
Vue.
version
)
;
2. nextTick()
-
Function: Executes a callback after the next DOM update cycle ends.
-
Usage:
nextTick(callback) -
Parameters:
-
callback: The callback function.
callback: The callback function.
Example
import
{
nextTick
}
from
'vue'
;
nextTick
(
(
)
=>
{
console.
log
(
'DOM updated'
)
;
}
)
;
3. defineComponent()
-
Function: Defines a component.
-
Usage:
defineComponent(options) -
Parameters:
-
options: The component options.
options: The component options.
Example
import
{
defineComponent
}
from
'vue'
;
const
MyComponent
=
defineComponent
(
{
template
:
'
}
)
;
4. defineAsyncComponent()
-
Function: Defines an async component.
-
Usage:
defineAsyncComponent(loader) -
Parameters:
-
loader: The async loading function.
loader: The async loading function.
Example
import
{
defineAsyncComponent
}
from
'vue'
;
const
AsyncComponent
=
defineAsyncComponent
(
(
)
=>
import
(
'./MyComponent.vue'
)
)
;