vue 1.x 交互-仿百度下拉列表

0、前言
vue 自己不支持交互,想要作交互,必须引入ajax 模块。vue 团队提供一个新的库文件叫作 vue-resource.js 。
网络CDN:https://cdn.bootcss.com/vue-r...css

一、用法分类
ajax 交互一般分为3类,get,post,jsonp
html 部分的代码:数组myData 的数据经过ul 列表显示出来,用"v-for"指令html

<body> 
       <div id="box">
           <input type="text" value="" v-model="m" @keyup="get()">
           {{m}}<br/>
           {{msg}}<br/>
           {{'welcome'|uppercase}}
           <ul>
               <li v-for="value in myData">
                   {{value}}
               </li>
           </ul>
           <p v-show="myData.length == 0">暂无数据</p>
       </div> 
</body>

1) get 请求vue

methods:{
    get: function(){
        this.$http.get('search',{
            wd:this.m
    }).then(function(res){
    this. myData= res.body
    },function(res){
    console.log(res.status)
})
}
    }

2)post 请求ajax

methods:{get : function () {
                    this.$http.post('search',{
                        wd:this.m
                    },{
                        emulateJSON:true,     //在get 请求的基础上添加了第3个参数
                    }).then(function(res){
                        this.myData=res.body;
                    },function(res){
                       console.log('err---');
                        // alert(2)
                        //this.myData = ['aaa', 'a111', 'a222'];
                    })
                   
               }}

在后台项目中,调试运行结果以下:
图片描述json

输入关键字“a”后,进入断点,获取数据:
图片描述跨域

3)jsonp 可以发送跨域请求,用的很少,不在此赘述数组

二、总结:
本片文章要求掌握get 和post 请求的写法,v-model 双向绑定数据,列表中运用v-for显示数组的数据,v-show 后面接条件控制数据显示与否网络

相关文章
相关标签/搜索