# Application API

In Vue 3, APIs that globally mutate Vue's behavior are now moved to application instances created by the new createApp method. In addition, their effects are now scoped to that specific application's instance:

import { createApp } from 'vue'

const app = createApp({})
1
2
3

Calling createApp returns an application instance. This instance provides an application context. The entire component tree mounted by the application instance share the same context, which provides the configurations that were previously "global" in Vue 2.x.

In addition, since the createApp method returns the application instance itself, you can chain other methods after it which can be found in the following sections.

# component

  • Arguments:

    • {string} name
    • {Function | Object} definition (optional)
  • Returns:

    • The application instance if a definition argument was passed
    • The component definition if a definition argument was not passed
  • Usage:

    Register or retrieve a global component. Registration also automatically sets the component's name with the given name parameter.

  • Example:

import { createApp } from 'vue'

const app = createApp({})

// register an options object
app.component('my-component', {
  /* ... */
})

// retrieve a registered component
const MyComponent = app.component('my-component')
1
2
3
4
5
6
7
8
9
10
11

# config

  • Usage:

An object containing application configurations.

  • Example:
import { createApp } from 'vue'
const app = createApp({})

app.config = {...}
1
2
3
4

# directive

  • Arguments:

    • {string} name
    • {Function | Object} definition (optional)
  • Returns:

    • The application instance if a definition argument was passed
    • The directive definition if a definition argument was not passed
  • Usage:

    Register or retrieve a global directive.

  • Example:

import { createApp } from 'vue'
const app = createApp({})

// register
app.directive('my-directive', {
  // Directive has a set of lifecycle hooks:
  // called before bound element's attributes or event listeners are applied
  created() {},
  // called before bound element's parent component is mounted
  beforeMount() {},
  // called when bound element's parent component is mounted
  mounted() {},
  // called before the containing component's VNode is updated
  beforeUpdate() {},
  // called after the containing component's VNode and the VNodes of its
  // children have updated
  updated() {},
  // called before the bound element's parent component is unmounted
  beforeUnmount() {},
  // called when the bound element's parent component is unmounted
  unmounted() {}
})

// register (function directive)
app.directive('my-directive', () => {
  // this will be called as `mounted` and `updated`
})

// getter, return the directive definition if registered
const myDirective = app.directive('my-directive')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

Directive hooks are passed these arguments:

# el

The element the directive is bound to. This can be used to directly manipulate the DOM.

# binding

An object containing the following properties.

  • instance: The instance of the component where directive is used.
  • value: The value passed to the directive. For example in v-my-directive="1 + 1", the value would be 2.
  • oldValue: The previous value, only available in beforeUpdate and updated. It is available whether or not the value has changed.
  • arg: The argument passed to the directive, if any. For example in v-my-directive:foo, the arg would be "foo".
  • modifiers: An object containing modifiers, if any. For example in v-my-directive.foo.bar, the modifiers object would be { foo: true, bar: true }.
  • dir: an object, passed as a parameter when directive is registered. For example, in the directive
app.directive('focus', {
  mounted(el) {
    el.focus()
  }
})
1
2
3
4
5

dir would be the following object:

{
  mounted(el) {
    el.focus()
  }
}
1
2
3
4
5

# vnode

A blueprint of the real DOM element received as el argument above.

# prevNode

The previous virtual node, only available in the beforeUpdate and updated hooks.

Note

Apart from el, you should treat these arguments as read-only and never modify them. If you need to share information across hooks, it is recommended to do so through element's dataset (opens new window).

# mixin

  • Arguments:

    • {Object} mixin
  • Returns:

    • The application instance
  • Usage:

    Apply a mixin in the whole application scope. Once registered they can be used in the template of any component within the current application. This can be used by plugin authors to inject custom behavior into components. Not recommended in application code.

  • See also: Global Mixin

# mount

  • Arguments:

    • {Element | string} rootContainer
    • {boolean} isHydrate (optional)
  • Returns:

    • The root component instance
  • Usage:

    The innerHTML of the provided DOM element will be replaced with the rendered template of the application root component.

  • Example:

<body>
  <div id="my-app"></div>
</body>
1
2
3
import { createApp } from 'vue'

const app = createApp({})
// do some necessary preparations
app.mount('#my-app')
1
2
3
4
5

# provide

  • Arguments:

    • {string | Symbol} key
    • value
  • Returns:

    • The application instance
  • Usage:

    Sets a value that can be injected into all components within the application. Components should use inject to receive the provided values.

    From a provide/inject perspective, the application can be thought of as the root-level ancestor, with the root component as its only child.

    This method should not be confused with the provide component option or the provide function in the composition API. While those are also part of the same provide/inject mechanism, they are used to configure values provided by a component rather than an application.

    Providing values via the application is especially useful when writing plugins, as plugins typically wouldn't be able to provide values using components. It is an alternative to using globalProperties.

    Note

    The provide and inject bindings are NOT reactive. This is intentional. However, if you pass down a reactive object, properties on that object do remain reactive.

  • Example:

    Injecting a property into the root component, with a value provided by the application:

import { createApp } from 'vue'

const app = createApp({
  inject: ['user'],
  template: `
    <div>
      {{ user }}
    </div>
  `
})

app.provide('user', 'administrator')
1
2
3
4
5
6
7
8
9
10
11
12

# unmount

  • Usage:

    Unmounts a root component of the application instance.

  • Example:

<body>
  <div id="my-app"></div>
</body>
1
2
3
import { createApp } from 'vue'

const app = createApp({})
// do some necessary preparations
app.mount('#my-app')

// Application will be unmounted 5 seconds after mount
setTimeout(() => app.unmount(), 5000)
1
2
3
4
5
6
7
8

# use

  • Arguments:

    • {Object | Function} plugin
    • ...options (optional)
  • Returns:

    • The application instance
  • Usage:

    Install a Vue.js plugin. If the plugin is an Object, it must expose an install method. If it is a function itself, it will be treated as the install method.

    The install method will be called with the application as its first argument. Any options passed to use will be passed on in subsequent arguments.

    When this method is called on the same plugin multiple times, the plugin will be installed only once.

  • Example:

    import { createApp } from 'vue'
    import MyPlugin from './plugins/MyPlugin'
    
    const app = createApp({})
    
    app.use(MyPlugin)
    app.mount('#app')
    
    1
    2
    3
    4
    5
    6
    7
  • See also: Plugins

# version

  • Usage:

    Provides the installed version of Vue as a string. This is especially useful for community plugins, where you might use different strategies for different versions.

  • Example:

    export default {
      install(app) {
        const version = Number(app.version.split('.')[0])
    
        if (version < 3) {
          console.warn('This plugin requires Vue 3')
        }
    
        // ...
      }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
  • See also: Global API - version