Model
- View
- ViewModel
Model:
data
data inView:
Template parsing->DOM
pageViewModel:
Vue
instance object
vm
The content of can be Vue
all the content of the prototype
data brokerObject.defineproperty(参数1, 参数2, 参数3)
- Parameter 1: object
- Parameter 2: attribute name
- Parameter 3: Configuration item
Function example: defining variables
let number = 18
let person = {
name = "zhangsan"
}
Function Example: Data Broker
Object.defineProperty(person, 'age', {
get: function(){
return number
}
})
Data agent features:
- Not enumerable, cannot participate in traversal, solution:
enumerable
set the attribute totrue
, the default is false - Unmodifiable, solution:
writable
set the attribute totrue
, the default is false - Cannot be deleted, solution:
configurable
set the attribute totrue
, the default is false
Data proxy functions:
- Limit the data and see its characteristics
get
Function: When the attribute of parameter 1 (object) is read,get
the function will be called, and the return value is the value of parameter 2 (attribute name). TheInvoke Property getter
mapping attributeget
function is calledgetter
The setter can be understood in the same way:
When modifying parameter 2 (property name) of the object, the setter function will be called and the modified value will be received.
Object.defineProperty(person, 'age', {
get: function(){
return number
}
set(value){
number = value
}
})
Data proxy usage examples
let obj = {
x:100}
let obj2 = {
y:200}
Object.defineProperty(obj2, 'x', {
get(){
return obj.x
}
set(value){
return obj.x = value
}
})
Vue
data broker
- By
vm
readingname
, inputtingvm.name
, callinggetter
, what is read isvm._data.name
inname
- By
vm
changingname
, changingvm.name
, callingsetter
, what is changed isvm._data.name
inname
Verification vm._data
is in the build parametersdata
vm
Store data
the data in vm._data
, vm._data
and the data hijacking cannot be directly verified. The data hijacking is to achieve responsiveness.
let data = {
name = "zhangsan"
}
const vm = new Vue({
el: '#root',
data
})
Verifiablevm._data === data
Vue
Data Broker Deep Understanding
-
vm
Novm.name
, only , but it can be accessedvm._data
through the data agent via inputvm.name
vm._data.name
-
Without a data agent, the interpolation must be written as
{ {_data.name}}
-
vm
Get it_data
, add an attributename
,getter
read_data
itname
,setter
modify_data
itname