vue中的數據監聽以及數據交互


現在我們來看一下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>

這樣一個簡單的小案例就做好了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM