Vue Instance Options
Options in Vue are the different options available on a Vue instance when using the Options API.
Some common options for a Vue instance:
-
el - Specifies the DOM element to which the Vue instance is mounted. If not provided, the Vue instance will not mount automatically.
-
data - Defines the data object of the Vue instance. The Vue instance's template, computed properties, methods, watchers, etc. can all access this data.
-
computed - Contains computed properties whose values are cached based on their dependencies; they only recompute when a dependency changes.
-
methods - Defines the methods of the Vue instance, callable from the template via event listeners.
-
watch - Contains one or more watchers used to observe and respond to data changes in the Vue instance.
-
props - Defines external data the component can receive.
-
components - Allows registering custom components in the Vue instance.
-
directives - Allows registering global custom directives.
-
filters - Defines filters usable in templates for text formatting.
-
model - Specifies the custom v-model property and event used by the component.
-
provide/inject - Provides dependency injection capability, allowing data to be passed across components.
-
lifecycle hooks - Includes
onMounted,onUpdated,onUnmountedand other lifecycle hooks, used to execute code at different stages of the Vue instance. -
template - Defines the HTML template of the Vue instance.
-
render function - Allows using JavaScript functions to render the component, providing more advanced customization.
-
errorCaptured - Used to globally capture errors from the Vue instance.
-
emits - Defines events the component can emit.
-
setup - The entry function in Vue 3's Composition API, used to organize component logic.
el
Mount point, used to specify the DOM element the Vue instance mounts to:
Example
const
app
=
Vue.
createApp
(
{
// options
}
)
;
app.
mount
(
'#app'
)
;
data
-
Type: Function or object
-
Purpose:
datais where component data is stored in a Vue instance. You can definedatavarious data properties in it and use them on the page via template syntax.dataThe data in it updates reactively—when the data changes, the related page content updates automatically.
Example
const
app
=
Vue.
createApp
(
{
data
(
)
{
return
{
message
:
'Hello Vue!'
}
;
}
}
)
;
computed
-
Type: Object
-
Purpose:
computedThe property allows you to declare computed properties based on reactive dependencies. A computed property only re-evaluates when its related dependencies change and caches the result, avoiding unnecessary recomputation.
Example
const
app
=
Vue.
createApp
(
{
data
(
)
{
return
{
firstName
:
'John'
,
lastName
:
'Doe'
}
;
}
,
computed
:
{
fullName
(
)
{
return
`$
{
this
.
firstName
}
$
{
this
.
lastName
}
`
;
}
}
}
)
;
methods
-
Type: Object
-
Purpose:
methodsThe property holds the methods of the Vue instance. These methods can be called in the template or in other instance methods. Unlike computed properties, methods re-execute on every call, making them suitable for responding to events or performing async operations.
Example
const
app
=
Vue.
createApp
(
{
data
(
)
{
return
{
count
:
0
}
;
}
,
methods
:
{
increment
(
)
{
this
.
count
++;
}
}
}
)
;
watch
-
Type: Object or function
-
Purpose:
watchThe property is used to observe and respond to data changes in the Vue instance. When the watched data changes, the specified callback executes. It is suitable for handling async or complex data-change logic.
Example
const
app
=
Vue.
createApp
(
{
data
(
)
{
return
{
message
:
'Hello Vue!'
}
;
}
,
watch
:
{
message
(
newValue
,
oldValue
)
{
console.
log
(
`Message changed from $
{
oldValue
}
to $
{
newValue
}
`
)
;
}
}
}
)
;
props
-
Type: Array or object
-
Purpose:
propsThe property is used to receive data passed from parent to child components. The child component receivespropsthe data passed from the parent and can use it internally.
In the following example, MyComponent receives a title prop and uses it in the template:
Example
const
MyComponent
=
{
props
:
[
'title'
]
,
template
:
'
{{ title }}
'}
;
const
app
=
Vue.
createApp
(
{
components
:
{
'my-component'
:
MyComponent
}
,
template
:
'
}
)
;
app.
mount
(
'#app'
)
;
template
-
Type: String
-
Purpose:
templateThe property defines the template structure of a Vue component. The template can use Vue's template syntax, including interpolation, directives, and computed properties. The template is eventually compiled into a render function that generates the component's virtual DOM.
Example
const
app
=
Vue.
createApp
(
{
data
(
)
{
return
{
message
:
'Hello Vue!'
}
;
}
,
template
:
'
}
)
;
components
components is used to register local components.
Example
const
ChildComponent
=
{
template
:
'
}
;
const
app
=
Vue.
createApp
(
{
components
:
{
'child-component'
:
ChildComponent
}
,
template
:
'
}
)
.
mount
(
'#app'
)
;
provide / inject
The parent component provides data, and the child component injects and uses it.
Example
const
ChildComponent
=
{
inject
:
[
'message'
]
,
template
:
'
}
;
const
app
=
Vue.
createApp
(
{
provide
(
)
{
return
{
message
:
'Provided Message'
}
;
}
,
components
:
{
'child-component'
:
ChildComponent
}
,
template
:
'
}
)
.
mount
(
'#app'
)
;
Lifecycle Hooks
beforeCreate
Called before the instance is initialized.
Example
const
app
=
Vue.
createApp
(
{
beforeCreate
(
)
{
console.
log
(
'beforeCreate'
)
;
}
}
)
.
mount
(
'#app'
)
;
created
Called after the instance is created.
Example
const
app
=
Vue.
createApp
(
{
created
(
)
{
console.
log
(
'created'
)
;
}
}
)
.
mount
(
'#app'
)
;
beforeMount
Called before mounting.
Example
const
app
=
Vue.
createApp
(
{
beforeMount
(
)
{
console.
log
(
'beforeMount'
)
;
}
}
)
.
mount
(
'#app'
)
;
mounted
Called after the instance is mounted.
Example
const
app
=
Vue.
createApp
(
{
mounted
(
)
{
console.
log
(
'mounted'
)
;
}
}
)
.
mount
(
'#app'
)
;
beforeUpdate
Called before data updates.
Example
const
app
=
Vue.
createApp
(
{
data
(
)
{
return
{
message
:
'Hello'
}
;
}
,
beforeUpdate
(
)
{
console.
log
(
'beforeUpdate'
)
;
}
}
)
.
mount
(
'#app'
)
;
updated
Called after data updates.
Example
const
app
=
Vue.
createApp
(
{
data
(
)
{
return
{
message
:
'Hello'
}
;
}
,
updated
(
)
{
console.
log
(
'updated'
)
;
}
}
)
.
mount
(
'#app'
)
;
beforeUnmount
Called before the instance is unmounted.
Example
const
app
=
Vue.
createApp
(
{
beforeUnmount
(
)
{
console.
log
(
'beforeUnmount'
)
;
}
}
)
.
mount
(
'#app'
)
;
unmounted
Called after the instance is unmounted.
Example
const
app
=
Vue.
createApp
(
{
unmounted
(
)
{
console.
log
(
'unmounted'
)
;
}
}
)
.
mount
(
'#app'
)
;
Other Options
directives
Registers local directives.
Example
const
app
=
Vue.
createApp
(
{
directives
:
{
focus
:
{
mounted
(
el
)
{
el.
focus
(
)
;
}
}
}
,
template
:
''
}
)
.
mount
(
'#app'
)
;
mixins
Mixin object.
Example
const
myMixin
=
{
created
(
)
{
console.
log
(
'Mixin created'
)
;
}
}
;
const
app
=
Vue.
createApp
(
{
mixins
:
[
myMixin
]
,
created
(
)
{
console.
log
(
'Component created'
)
;
}
}
)
.
mount
(
'#app'
)
;
extends
Extends another component.
Example
const
BaseComponent
=
{
created
(
)
{
console.
log
(
'BaseComponent created'
)
;
}
}
;
const
app
=
Vue.
createApp
(
{
extends
:
BaseComponent
,
created
(
)
{
console.
log
(
'Extended component created'
)
;
}
}
)
.
mount
(
'#app'
)
;
inheritAttrs
Whether to inherit the parent component's attributes.
Example
const
MyComponent
=
{
inheritAttrs
:
false
,
props
:
[
'title'
]
,
template
:
'
{{ title }}
}
;
const
app
=
Vue.
createApp
(
{
components
:
{
'my-component'
:
MyComponent
}
,
template
:
'
}
)
.
mount
(
'#app'
)
;
setup
Used for the Composition API.
Example
const
app
=
Vue.
createApp
(
{
setup
(
)
{
const
message
=
Vue.
ref
(
'Hello Vue 3!'
)
;
return
{
message
}
;
}
,
template
:
'
{{ message }}
'}
)
.
mount
(
'#app'
)
;
expose
In Vue 3's Composition API, when using the setup function to define component logic, you can use the expose method to explicitly specify the properties and methods to expose externally.
Here is a simple example showing how to use expose to expose properties and methods in a Vue 3 component:
Parent component (ParentComponent.vue)
Call Child Method
Child component (ChildComponent.vue)
{{ exposedData }}
Explanation:
-
Parent component:
ref creates a reference to the child component child.callChildMethod ; in the method, through child.value access the child component instance and call the method exposed by the child component exposedMethod.-
Child component:
setup function, through ref creates a reactive data exposedData.exposedMethod, used to demonstrate how to expose a method.expose The method explicitly exposes exposedData and exposedMethod to the parent component.exposedData, so it can be used in the template.