Global API: General
version
Exposes the current version of Vue.
Type:
string
Example
jsimport { version } from 'vue' console.log(version)
nextTick()
A utility for waiting for the next DOM update flush.
Type
tsfunction nextTick(callback?: () => void): Promise<void>
Details
When you mutate reactive state in Vue, the resulting DOM updates are not applied synchronously. Instead, Vue buffers them until the "next tick" to ensure that each component updates only once no matter how many state changes you have made.
nextTick()
can be used immediately after a state change to wait for the DOM updates to complete. You can either pass a callback as an argument, or await the returned Promise.Example
vue<script setup> import { ref, nextTick } from 'vue' const count = ref(0) async function increment() { count.value++ // DOM not yet updated console.log(document.getElementById('counter').textContent) // 0 await nextTick() // DOM is now updated console.log(document.getElementById('counter').textContent) // 1 } </script> <template> <button id="counter" @click="increment">{{ count }}</button> </template>
See also:
this.$nextTick()
defineComponent()
A type helper for defining a Vue component with type inference.
Type
tsfunction defineComponent( component: ComponentOptions | ComponentOptions['setup'] ): ComponentConstructor
Type is simplified for readability.
Details
The first argument expects a component options object. The return value will be the same options object, since the function is essentially a runtime no-op for type inference purposes only.
Note that the return type is a bit special: it will be a constructor type whose instance type is the inferred component instance type based on the options. This is used for type inference when the returned type is used as a tag in TSX.
You can extract the instance type of a component (equivalent to the type of
this
in its options) from the return type ofdefineComponent()
like this:tsconst Foo = defineComponent(/* ... */) type FooInstance = InstanceType<typeof Foo>
Note on webpack Treeshaking
Because
defineComponent()
is a function call, it could look like that it would produce side-effects to some build tools, e.g. webpack. This will prevent the component from being tree-shaken even when the component is never used.To tell webpack that this function call is safe to be tree-shaken, you can add a
/*#__PURE__*/
comment notation before the function call:jsexport default /*#__PURE__*/ defineComponent(/* ... */)
Note this is not necessary if you are using Vite, because Rollup (the underlying production bundler used by Vite) is smart enough to determine that
defineComponent()
is in fact side-effect-free without the need for manual annotations.See also: Guide - Using Vue with TypeScript
defineAsyncComponent()
Define an async component which is lazy loaded only when it is rendered. The argument can either be a loader function, or an options object for more advanced control of the loading behavior.
Type
tsfunction defineAsyncComponent( source: AsyncComponentLoader | AsyncComponentOptions ): Component type AsyncComponentLoader = () => Promise<Component> interface AsyncComponentOptions { loader: AsyncComponentLoader loadingComponent?: Component errorComponent?: Component delay?: number timeout?: number suspensible?: boolean onError?: ( error: Error, retry: () => void, fail: () => void, attempts: number ) => any }
See also: Guide - Async Components
defineCustomElement()
This method accepts the same argument as defineComponent
, but instead returns a native Custom Element class constructor.
Type
tsfunction defineCustomElement( component: | (ComponentOptions & { styles?: string[] }) | ComponentOptions['setup'] ): { new (props?: object): HTMLElement }
Type is simplified for readability.
Details
In addition to normal component options,
defineCustomElement()
also supports a special optionstyles
, which should be an array of inlined CSS strings, for providing CSS that should be injected into the element's shadow root.The return value is a custom element constructor that can be registered using
customElements.define()
.Example
jsimport { defineCustomElement } from 'vue' const MyVueElement = defineCustomElement({ /* component options */ }) // Register the custom element. customElements.define('my-vue-element', MyVueElement)
See also:
Also note that
defineCustomElement()
requires special config when used with Single-File Components.