Vue.js (original) (raw)

Global API: General

version

Exposes the current version of Vue.

import { version } from 'vue'  
console.log(version)  

nextTick()

A utility for waiting for the next DOM update flush.

function nextTick(callback?: () => void): Promise<void>  
<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>  

vue

<script>  
import { nextTick } from 'vue'  
export default {  
  data() {  
    return {  
      count: 0  
    }  
  },  
  methods: {  
    async increment() {  
      this.count++  
      // 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>  

defineComponent()

A type helper for defining a Vue component with type inference.

// options syntax  
function defineComponent(  
  component: ComponentOptions  
): ComponentConstructor  
// function syntax (requires 3.3+)  
function defineComponent(  
  setup: ComponentOptions['setup'],  
  extraOptions?: ComponentOptions  
): () => any  

Type is simplified for readability.

const Foo = defineComponent(/* ... */)  
type FooInstance = InstanceType<typeof Foo>  

Function Signature

import { ref, h } from 'vue'  
const Comp = defineComponent(  
  (props) => {  
    // use Composition API here like in <script setup>  
    const count = ref(0)  
    return () => {  
      // render function or JSX  
      return h('div', count.value)  
    }  
  },  
  // extra options, e.g. declare props and emits  
  {  
    props: {  
      /* ... */  
    }  
  }  
)  

The main use case for this signature is with TypeScript (and in particular with TSX), as it supports generics:
tsx

const Comp = defineComponent(  
  <T extends string | number>(props: { msg: T; list: T[] }) => {  
    // use Composition API here like in <script setup>  
    const count = ref(0)  
    return () => {  
      // render function or JSX  
      return <div>{count.value}</div>  
    }  
  },  
  // manual runtime props declaration is currently still needed.  
  {  
    props: ['msg', 'list']  
  }  
)  

In the future, we plan to provide a Babel plugin that automatically infers and injects the runtime props (like for defineProps in SFCs) so that the runtime props declaration can be omitted.

Note on webpack Treeshaking

Because defineComponent() is a function call, it could look like 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:
js

export 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.

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.

function 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  
}