A small example of data monitoring and data interaction in vue

巴扎黑
Release: 2017-07-21 17:21:21
Original
1706 people have browsed it

Now let’s take a look at the data monitoring event $watch in vue,

js code:

 new Vue({
                el:"#div",
                data:{
                   arr:[1,2,3]
                }
            }).$watch("arr",function () {
                alert("数据改变了")
            })
Copy after login

html code:

<div id="div">
<input type="button" value="改变" @click="arr.push(5)">
<h1>{{arr}}</h1>
</div>
Copy after login

This is the monitoring of the array. We are the same for json, but we have to give it a deep monitoring, the third parameter of $watch is {deep: true}.

The data interaction in angular includes $http. Similarly for vue, we also have data interaction, including post, get and jsonp methods.

We are doing a simple Baidu search function here

css code:

 a{
            text-decoration: none;
            color: black;
        }
        #div{
            text-align: center;
            padding-top: 50px;
        }
        input{
            height: 25px;
            width: 500px;
            border-radius: 5px;
            outline: none;
        }
        ul{
            margin-left:470px;
            margin-top: 0;
        }
        li{
            height: 25px;
            text-align: left;
            border:1px solid gray;
            list-style: none;
            width: 500px;
        }
Copy after login

js code:

new Vue({
                el:"#div",
                data:{
                    msg:" ",
                    arr:[]
                },
                methods:{get:function () {this.$http.jsonp('',{
                            wd:this.msg
                        },{
                            jsonp: 'cb'}).then(function(res){this.arr=res.data.s
                        },function(s){
                            console.log(s);
                        });
                    }
                }
            })
Copy after login

html Code:

<div id="div">
<input type="text" v-model="msg" @keyup="get()">
<ul>
    <li v-for="item in arr"><a href="javascript:;">{{item}}</a></li>
</ul>
</div>
Copy after login

Such a simple small case is ready.

The above is the detailed content of A small example of data monitoring and data interaction in vue. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!