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

Vue3 Options API

Vue3 Options API

The following APIs are widely used in Vue's Options API for managing state, lifecycle, slots, dependency injection, and more.

1. State Options

No. API & Description Example
1 data
The component's data option, used to define the component's state. data() { return { count: 0 }; }
2 props
The component's props option, defining the data passed from parent to child components. props: { msg: String }
3 computed
Computed properties, used to define derived values based on component state (data, props). computed: { doubledCount() { return this.count * 2; } }
4 methods
The component's methods option, used to define the component's business logic. methods: { increment() { this.count += 1; } }
5 watch
Watches the component's reactive data changes and executes a callback. watch: { count(newVal) { console.log(newVal); } }
6 emits
Defines the custom events a component can emit. emits: ['update']
7 expose
Properties or methods explicitly exposed for the parent component to access. expose() { return { method } }

2. Rendering Options

No. API & Description Example
1 template
Defines the component's HTML template.
2 render
Uses a render function instead of a template. render(h) { return h('button', { on: { click: this.increment } }, this.count) }
3 compilerOptions
Configures template compiler options. compilerOptions: { delimiters: ['{{', '}}'] }
4 slots
Defines the component's slots.

3. Lifecycle Options

No. API & Description Example
1 beforeCreate
Called before the component is instantiated. beforeCreate() { console.log('Before Create') }
2 created
Called after the component instance is created, with data already set up. created() { console.log('Created') }
3 beforeMount
Called before mounting to the DOM, after the template is compiled. beforeMount() { console.log('Before Mount') }
4 mounted
Called after the component is mounted to the DOM. mounted() { console.log('Mounted') }
5 beforeUpdate
Called after data changes and before the component re-renders. beforeUpdate() { console.log('Before Update') }
6 updated
Called after the component update is complete. updated() { console.log('Updated') }
7 beforeUnmount
Called before the component is unmounted. beforeUnmount() { console.log('Before Unmount') }
8 unmounted
Called after the component is unmounted. unmounted() { console.log('Unmounted') }
9 errorCaptured
Called when capturing errors from child components. errorCaptured(err, vm, info) { console.error(err) }
10 renderTracked
Called when reactive data changes and re-renders. renderTracked(event) { console.log('Render Tracked') }
11 renderTriggered
Called when reactive data triggers a re-render. renderTriggered(event) { console.log('Render Triggered') }
12 activated
Called when the component is activated (only used in keep-alive components). activated() { console.log('Activated') }
13 deactivated
Called when the component is deactivated (only used in keep-alive components). deactivated() { console.log('Deactivated') }
14 serverPrefetch
A hook for fetching data during server-side rendering. serverPrefetch() { return fetchData() }

4. Composition Options

No. API & Description Example
1 provide
Provides dependencies to descendant components. provide('key', 'value')
2 inject
Injects dependencies from ancestor components. inject('key')
3 mixins
Imports external mixins to reuse logic. mixins: [myMixin]
4 extends
The component extends the options of another component. extends: MyComponent

5. Other Miscellaneous

No. API & Description Example
1 name
The component's name, usually used for debugging. name: 'MyComponent'
2 inheritAttrs
Controls whether the parent component's attributes (attrs) are automatically inherited by the root element. inheritAttrs: false
3 components
Registers a local component so it can be used in the current component. components: { LocalComponent }
4 directives
Registers a local directive so it can be used in the current component. directives: { focus: FocusDirective }

6. Component Instance

No. API & Description Example
1 $data
Gets the component's data object. this.$data.count
2 $props
Gets the component's props object. this.$props.msg
3 $el
Gets the DOM element the component is mounted to. this.$el
4 $options
Gets the component's configuration options. this.$options.name
5 $parent
Gets the parent component instance. this.$parent
6 $root
Gets the root component instance. this.$root
7 $slots
Gets the slot content of a component. this.$slots.default
8 $refs
Gets the component's refs object. this.$refs.input
9 $attrs
Gets all attributes not bound to the component's props. this.$attrs
10 $watch()
Watches the component instance's reactive data or computed properties. this.$watch('count', (newCount) => { console.log(newCount) })
11 $emit()
Emits a custom event. this.$emit('update', newVal)
12 $forceUpdate()
Forces Vue to re-render the component. this.$forceUpdate()
13 $nextTick()
Executes a callback after the next DOM update cycle ends. this.$nextTick(() => { console.log('DOM updated') })