Vue. js implements data pulling and updating using custom commands. vue. js customizes data pulling and updating.
Preface
The code snippet of this article is located in the single file component of vue, that is. in the file at the end of vue, this article describes only one implementation method. It is neither the only method nor the best method. If you have a better method, leave a message for discussion.
Step 1
First, you must first define the variables:
// App. vue <script> data () {return {// defines getData :{}, // defines the binding value of the custom command ifUpdate: true }}
Step 2
Then, to use ajax, introduce jquery in index.html so that you can use it globally:
// index.html<script type="text/javascript" src="./lib/js/jquery-2.1.1.min.js"></script>
Step 3
Then, a custom command is used to automatically execute the methods in the Custom command after the component is instantiated:
After the component is instantiated, it will immediately start with an initial valueifUpdate
This is the first time a custom command is called for a parameter.initData
Method, followed by each parameter valueifUpdate
It will be called again when it changesinitData
The parameter isifUpdate
And the old value.
// App. vue <template> // bind custom commands to the page node (select as needed) <div v-initData = "ifUpdate"> </div>
// App. vue <script> // define the custom directive directives: {initData: function (val, oldVal) {if (! Val) {return;} var self = this; $. getJSON ("ajax/test. json ", function (data) {self. vm. getData = data;}); // when the value of ifUpdate is updated to true next time, the above ajax will be used again to obtain the data self. vm. ifUpdate = false ;}}
Use Cases
After the user comments, refresh the Comments List:
After obtaining the comment data for the first timeifUpdate
Changefalse
After adding a comment, the user uploads the data to the background andifUpdate
Changetrue
Command accordingifUpdate
Triggered by changes, passed againajax
Pull data from the background
Pull after pullingifUpdate
Resetfalse
Summary
The above is all the content of this article. I hope this article will be helpful for you to learn or use vue. js. If you have any questions, please leave a message.