vue html数据库交互,vue中有关数据监听与数据交互的小例子

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

js代码:

new Vue({

el:"#div",

data:{

arr:[1,2,3]

}

}).$watch("arr",function () {

alert("数据改变了")

})

html代码:

{{arr}}

这就是数组的监听,对于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('',{

wd:this.msg

},{

jsonp: 'cb'}).then(function(res){this.arr=res.data.s

},function(s){

console.log(s);

});

}

}

})

html代码:

  • {{item}}

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