现在我们来看一下vue中的数据监听事件$watch,

js代码:

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

html代码:

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

这就是数组的监听,对于json我们也是一样的,但是我们得给他一个深度监听,$watch的第三个参数{deep:true}。

angular 中的数据交互有$http,同样对于vue我们也是有数据交互的,有post,get以及jsonp的方法。

我们在这里做一个简单的百度搜索功能

css代码:

 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;
        }

 

js代码:

new Vue({
                el:"#div",
                data:{
                    msg:" ",
                    arr:[]
                },
                methods:{
                    get:function () {
                        this.$http.jsonp('https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?',{
                            wd:this.msg
                        },{
                            jsonp: 'cb'
                        }).then(function(res){
                            this.arr=res.data.s
                        },function(s){
                            console.log(s);
                        });
                    }
                }
            })

html代码:

<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>

这样一个简单的小案例就做好了。

 

posted on 2017-07-11 22:12  Hero^  阅读(6573)  评论(4编辑  收藏  举报