时间:2021-07-01 10:21:17 帮助过:3人阅读
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 部分的代码:数组myData 的数据通过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">暂无数据</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个参数
- }).then(function(res){
- this.myData=res.body;
- },function(res){
- console.log('err---');
- // alert(2)
- //this.myData = ['aaa', 'a111', 'a222'];
- })
- }}
在后台项目中,调试运行结果如下:
输入关键字“a”后,进入断点,获取数据:
3)jsonp 能够发送跨域请求,用的不多,不在此赘述
2、总结:
本片文章要求掌握get 和post 请求的写法,v-model 双向绑定数据,列表中运用v-for显示数组的数据,v-show 后面接条件控制数据显示与否
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。