Vue. js implements data pulling and updating using custom commands. vue. js customizes data pulling and updating.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.