An example Analysis of vue2.0 responsive programming
This article introduces the relevant knowledge of "vue2.0 responsive programming case analysis". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Vue2.0 uses Object.defineProterty to traverse and convert attributes in data to getter and setter, and in getter a collection is made using the context of the data, which we call dependency collection.
In setter, the operation of dependency update will be triggered, so that there may be multiple changes in the template according to the dependency, so we collect all the places and perform a batch operation while waiting for the update.
Here is a demonstration of demo
(function () {
Let x
Let y
Let f = n = > n * 100 + 200
Let active
Let onXChanged = function (cb) {
Active = cb
Active ()
Active = null
}
/ / collect more dependencies
Class Dep {
/ / dependency collection, adding response dependencies to deps
Constructor () {
This.deps = new Set ()
}
Depend () {
If (active) {
This.deps.add (active)
}
}
/ / one pair depends on one execution in the current deps
Notify () {
This.deps.forEach (dep = > dep ())
}
}
Let ref = initValue = > {
Let value = initValue
Let dep = new Dep ()
Return Object.defineProperty ({}, "value", {
Get () {
Dep.depend ()
Return value
}
Set (newValue) {
Value = newValue
Dep.notify ()
}
})
}
X = ref (1)
OnXChanged () = > {
Y = f (x.value)
Console.log (y)
});
X.value = 2
X.value = 3
) ()
This is the end of the content of "vue2.0 responsive programming example Analysis". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!