VUE Response Update Mechanism and Do Not Use Simple Data Two – way Binding Problems

Recently, some people recently said that VUE is binding, some people say that VUE is unidirectional data stream, I think these two statements are wrong, VUE is a framework with a response update mechanism. The two-way binding of data can be achieved without both one-way data streams.

2 One-way data stream and data two-way binding

One-way data stream refers to the change in VIEW when the data in the model changes.

Bidirectional data binding refers to changes in VIEW when the data in Model changes, and the change of View can also cause modest changes. vue响应式更新机制及不使用框架实现简单的数据双向绑定问题

// This is a one-way data stream, and the value value of this INPUT cannot be a change in the TEXT attribute in the DATA.

Data: {return {text: ‘Text input box’}} // This is two-way data binding, whether modifying Model or modifying VIEW can cause another change. vue响应式更新机制及不使用框架实现简单的数据双向绑定问题 Data: {return {text: ‘text input box’}}

3 VUE data two-way binding implementation principle
   VUE provides us with two syntaxial sugars that implement data two-way binding, v-model and .sync modifiers, V-model for providing data bidirectional binding for the form element ,.sync modifier is used for Provide data bidirectional binding for any attribute, let us try the syntaxial sugar provided by Vue, and implement the data two-way binding.   in order to cause a change in model view is changed when the first listener to change the view, go to change the model when the view changes, the following is the code that implements Once you have the idea. 
// First, the change of the view is listened by the input event

DATA: {Return {TEXT: ‘Text Input Box’}}, Methods: {// The view is changed, the value of the view is given the value of the model, and the data bidister binding onInput (Event) {this.text = Event.Target.Value;}}

4 VUE in the unidirectional data flow implementation

VUE One-way data flows to
Object.defineProperty () this API.
  Object.defineProperty Usage:   
// Object.defineProperty is used for data hijacking, listening to reading and writing of a variable, and reading When writing, the callback function is executed Object.defineProperty (Obj, Prop, DESC);
// Obj is the object to be defined, the Prop is the name of the attribute to be defined, the desc is a descriptor of the property

// Defines an object and listening to the value operation and value operation of his Text attribute and the value operation Let Data = {}; Object. DefineProperty (DATA, ‘TEXT’, {GET () {Console.log (‘Value Action’);}, set (newval) {console.log (‘存 值 操作 操作 操作’);}}); Console.log ‘data’); ===> Output: ===> {} ===> Value operation data.text = ‘text input box’; ===> Output ===> save value operation

With Object.defineProperty () This API can monitor the changes in data in MODEL and modify the view to reach the one-way data stream when data changes.

5 Implement a simple data two-way binding

A simple data bidirectional binding, the target is to change the variables in Model changes in Model Modifying Model changes the view.

// HTML

// jslet input = document.queryselector (‘# INPUT’); // Defines Modellet Data = {Text : ”}; // Monitor the change of Text in Model, first implement the data unidirectional stream Object.defineProperty (Data, Text, {get () {}, // text When changing, modify the value of the INPUT element. (NEWVAL) {INPUT.VALUE = NewVal;}}); // Monitor the change of the input element and modify the value of Model, implement data bidirectional binding INPUT.ADDEVENTLISENER (‘INPUT’, Event => {data.text = Event. Target.value;};
   

The simple data bidirectional binding can be realized, and the value of the Data.Text can be modified in the console that changes changes. Modify the value of the Input element Print Data.Text in the console to see if MODEL changes.

The above is the VUE response update mechanism for Xiaobi introduced and does not use the framework to achieve simple data two-way binding problems, I hope to everyone HaveHelped, if you have any questions, please leave a message, Xiaobian will reply to everyone in time. Thank you very much for your support of Tumi Cloud Website!

If you think this article helps you, welcome to reprint, please indicate the source, thank you!
© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment