vue 1.x 交互實現(xiàn)仿百度下拉列表示例
0、前言
vue 本身不支持交互,想要做交互,必須引入ajax 模塊。vue 團隊提供一個新的庫文件叫做 vue-resource.js 。
網絡CDN:https://cdn.bootcss.com/vue-resource/1.3.4/vue-resource.js
1、用法分類
ajax 交互通常分為3類,get,post,jsonp
html 部分的代碼:數(shù)組myData 的數(shù)據(jù)通過ul 列表顯示出來,用"v-for"指令
<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">暫無數(shù)據(jù)</p> </div> </body>
1) get 請求
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 請求
methods:{get : function () { this.$http.post('search',{ wd:this.m },{ emulateJSON:true, //在get 請求的基礎上添加了第3個參數(shù) }).then(function(res){ this.myData=res.body; },function(res){ console.log('err---'); // alert(2) //this.myData = ['aaa', 'a111', 'a222']; }) }}
在后臺項目中,調試運行結果如下:
輸入關鍵字“a”后,進入斷點,獲取數(shù)據(jù):
3)jsonp 能夠發(fā)送跨域請求,用的不多,不在此贅述
2、總結:
本片文章要求掌握get 和post 請求的寫法,v-model 雙向綁定數(shù)據(jù),列表中運用v-for顯示數(shù)組的數(shù)據(jù),v-show 后面接條件控制數(shù)據(jù)顯示與否
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- vue基礎之使用get、post、jsonp實現(xiàn)交互功能示例
- VueJS組件之間通過props交互及驗證的方式
- vue中axios處理http發(fā)送請求的示例(Post和get)
- Vue resource中的GET與POST請求的實例代碼
- vue 2.x 中axios 封裝的get 和post方法
- vue axios數(shù)據(jù)請求get、post方法及實例詳解
- vue中axios的封裝問題(簡易版攔截,get,post)
- vuejs使用axios異步訪問時用get和post的實例講解
- Vue axios全局攔截 get請求、post請求、配置請求的實例代碼
- vue實現(xiàn)百度下拉列表交互操作示例
相關文章
使用vue3+ts+setup獲取全局變量getCurrentInstance的方法實例
這篇文章主要給大家介紹了關于使用vue3+ts+setup獲取全局變量getCurrentInstance的相關資料,文中通過實例代碼介紹的非常詳細,對大家學習或者使用vue3具有一定的參考學習價值,需要的朋友可以參考下2022-08-08