# 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]
  • 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 (always return constructor)
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]
  • 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 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

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.

# mixin

  • Arguments:

    • {Object} mixin
  • Usage:

    Apply a mixin in the whole application scope, which will affect every Vue instance created afterwards in the given app (for example, child components). 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
  • Usage:

    Mounts a root component of the application instance on the provided DOM element.

  • 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

  • Type:

    • Object | () => Object
  • Details:

    This option is used together with inject to allow an ancestor component to serve as a dependency injector for all its descendants, regardless of how deep the component hierarchy is, as long as they are in the same parent chain.

    The provide option should be an object or a function that returns an object. This object contains the properties that are available for injection into its descendants. You can use ES2015 Symbols as keys in this object, but only in environments that natively support Symbol and Reflect.ownKeys.

    Note: the provide and inject bindings are NOT reactive. This is intentional. However, if you pass down an observed object, properties on that object do remain reactive.

  • Example:

import { createApp } from 'vue'

const app = createApp({
  provide: {
    user: 'John Doe'
  }
})

app.component('user-card', {
  inject: ['user'],
  template: `
    <div>
      {{ user }}
    </div>
  `
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# unmount

  • Arguments:

    • {Element | string} rootContainer
  • Usage:

    Unmounts a root component of the application instance on the provided DOM element.

  • 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('#my-app'), 5000)
1
2
3
4
5
6
7
8

# use

  • Arguments:

    • {Object | Function} plugin
  • 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 Vue as the argument.

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

  • See also: Plugins

Deployed on Netlify.
Last updated: 7/19/2020, 3:48:09 PM