Vue3 data() Function
In Vue 3,data() The function is one of the core functions used to define a component's internal state (data).
data() The function returns an object whose properties are the component's internal data, accessible and manipulable through templates or other logic.
data() The function is an essential part of every Vue component; it is used to initialize the component's data state.
Basic Usage of the data() Function
In Vue 3,data() The function is typically defined as a function and returns an object. This object contains the data properties the component needs.
Example Code
Example
export
default
{
data
(
)
{
return
{
message
:
'Hello, Vue3!'
,
count
:
0
}
;
}
}
;
Explanation
data()The function returns an object containing two properties:message: A string-type data with an initial value of'Hello, Vue3!'.count: A number-type data with an initial value of0.
- These data can be used directly in the component's template or methods.
Why Is data() a Function?
In Vue 3,data() must be a function rather than directly returning an object. This is because a Vue component may be reused multiple times; if data() is an object, all instances would share the same data object, leading to data conflicts.
By defining data() as a function, every time a component instance is created, Vue calls this function and returns a new data object, ensuring each instance's data is independent.
Example
Example
export
default
{
data
(
)
{
return
{
message
:
'Hello, Vue3!'
}
;
}
}
;
- Each time a component instance is created,
data()the function is called and returns a brand-newmessagedata object.
Characteristics of the data() Function
1. Reactive Data
data() The properties in the returned object are converted by Vue into reactive data. When this data changes, Vue automatically updates the views that depend on it.
2. Data Initialization
data() The function is called when the component is instantiated, used to initialize the component's data state.
3. Data Isolation
Because data() is a function, each component instance has its own independent data object, avoiding data sharing and conflicts.
Notes on the data() Function
1. Do Not Use Arrow Functions
data() The function cannot use arrow functions, because arrow functions do not have their own this context, which would prevent correct access to the component instance.
Incorrect Example:
Example
export
default
{
data
:
(
)
=>
(
{
message
:
'Hello, Vue3!'
}
)
}
;
Correct Example:
Example
export
default
{
data
(
)
{
return
{
message
:
'Hello, Vue3!'
}
;
}
}
;
2. Avoid Directly Modifying Vue Instance Properties
in data() Within the function, do not directly modify Vue instance properties (such as this.$data), but instead define data by returning an object.
Practical Exercise
Try to create a Vue component and define the following data in the data() function:
username: initial value is an empty string''.age: initial value is0.isStudent: initial value isfalse.
Example
User information
Username: {{ username }}
Age: {{ age }}
Is a student: {{ isStudent ? 'Yes' : 'No' }}
Vue3 Options API