Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Vue 3 Global API

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()

-

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()

-

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()

-

container: A DOM element or selector string.

Example

app.

mount

(

'#app'

)

;


4. app.unmount()

Example

app.

unmount

(

)

;


5. app.onUnmount()

-

callback: The function called on unmount.

Example

app.

onUnmount

(

(

)

=>

{

  console.

log

(

'App unmounted'

)

;

}

)

;


6. app.component()

-

Register: app.component(name, component)

-

Get: app.component(name)

-

name: The component name.

-

component: The component definition.

Example

app.

component

(

'MyComponent'

,

{

  template

:

'

My Component
'

}

)

;


7. app.directive()

-

Register: app.directive(name, directive)

-

Get: app.directive(name)

-

name: The directive name.

-

directive: The directive definition.

Example

app.

directive

(

'focus'

,

{

  mounted

(

el

)

{

    el.

focus

(

)

;

 

}

}

)

;


8. app.use()

-

plugin: The plugin.

-

options (Optional): Plugin options.

Example

import

MyPlugin from

'./MyPlugin'

;

app.

use

(

MyPlugin

)

;


9. app.mixin()

-

mixin: The mixin object.

Example

app.

mixin

(

{

  created

(

)

{

    console.

log

(

'Global mixin created'

)

;

 

}

}

)

;


10. app.provide()

-

key: The dependency name.

-

value: The dependency value.

Example

app.

provide

(

'theme'

,

'dark'

)

;


11. app.runWithContext()

-

fn: The function to run.

Example

app.

runWithContext

(

(

)

=>

{

  console.

log

(

'Running with app context'

)

;

}

)

;


12. app.version

Example

console.

log

(

app.

version

)

;


13. app.config

Example

app.

config

.

errorHandler

=

(

err

)

=>

{

  console.

error

(

err

)

;

}

;


14. app.config.errorHandler

-

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

-

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

Example

app.

config

.

performance

=

true

;


17. app.config.compilerOptions

Example

app.

config

.

compilerOptions

=

{

whitespace

:

'preserve'

}

;


18. app.config.globalProperties

Example

app.

config

.

globalProperties

.$myGlobal

=

'Hello'

;


19. app.config.optionMergeStrategies

Example

app.

config

.

optionMergeStrategies

.

customOption

=

(

parent

,

child

)

=>

{

 

return

child

||

parent

;

}

;


20. app.config.idPrefix

Example

app.

config

.

idPrefix

=

'my-app-'

;


21. app.config.throwUnhandledErrorInProduction

Example

app.

config

.

throwUnhandledErrorInProduction

=

true

;


General API

1. version

Example

console.

log

(

Vue.

version

)

;


2. nextTick()

-

callback: The callback function.

Example

import

{

nextTick

}

from

'vue'

;

nextTick

(

(

)

=>

{

  console.

log

(

'DOM updated'

)

;

}

)

;


3. defineComponent()

-

options: The component options.

Example

import

{

defineComponent

}

from

'vue'

;

const

MyComponent

=

defineComponent

(

{

  template

:

'

My Component
'

}

)

;


4. defineAsyncComponent()

-

loader: The async loading function.

Example

import

{

defineAsyncComponent

}

from

'vue'

;

const

AsyncComponent

=

defineAsyncComponent

(

(

)

=>

 

import

(

'./MyComponent.vue'

)

)

;