温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在Vue中调取接口并渲染数据

发布时间:2021-03-23 16:00:12 来源:亿速云 阅读:826 作者:Leah 栏目:web开发

这篇文章将为大家详细讲解有关怎么在Vue中调取接口并渲染数据,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

首先,在HTML页面引入:

//引入vue.js文件 <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script> 引入vue-resource.min.js文件,就可以引入接口方法了 <script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js"></script>

然后,在body中书写div:

//id在下面js中进行引用 <div id="box">    <table border="1" cellpadding="0" cellspacing="0">  <tr>  <td>序号</td>  <td>姓名</td>  <td>头像</td>  </tr>   //v-for 循环数据表中的数据  <tr v-for="v in msg">  <td>{{v.id}}</td>  <td>{{v.username}}</td>   <td>{{v.photo}}</td>  </tr>  </table> </div>

第三,js代码:

<script type = "text/javascript"> window.onload = function(){ //实例化vue类 var vm = new Vue({  //绑定box  el:'#box',  data:{    //设置msg内容为空,在请求数据前为空的状态    msg:'',    },  mounted:function () {    //调取本地的get(就在下面)    this.get();    },  methods:{  get:function(){    //发送get请求    this.$http.post('http://你的IP/api/方法',{key:"密钥"},{emulateJSON:true}).then(function(res){     //msg等于回调函数返回的res(值)     this.msg=res.body.data;     //在打印台测试打印,无误后一定要删除     console.log(res);      },function(){     console.log('请求失败处理');    });   }  } }); } </script>

控制器:

public function index()  {   //  //引入秘钥   $pwd=new ApisModel();   $passwd=$pwd->passwd();   // print_r($passwd);die;   //空的数组,等待输入秘钥与存储在model层的秘钥对比   $date=request()->get();    // print_r($date);die;   // 对比秘钥是否一致   if($date['key']==$passwd){     $model=new ApisModel();     $data=$model->role_show();         return json(array('data'=>$data,'code'=>1,'message'=>'操作完成'));    }else{     $data = ['name'=>'status','message'=>'操作失败'];          return json(['data'=>$data,'code'=>2,'message'=>'秘钥不正确']);    }    }

model:

public function passwd(){  $key='存放在本地的密钥';   return $key;  }  //简单的测试接口  public function role_show(){   return Db::name('role_power')->select();    }

OK,post方式搞定了,下面是vue使用get方法进行接口调用,渲染数据

简单粗暴,大致一样,就不一一详解了,上代码:

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Vue 测试实例 - 菜鸟教程(runoob.com)</title> <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script> <script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js"></script> </head> <body>   <div id="box">    <table border="1" cellpadding="0" cellspacing="0">  <tr>  <td >ROLE_ID</td>  <td >POWER_ID</td>  <td >创建时间</td>  </tr>  <tr v-for="v in msg">  <td >{{v.role_id}}</td>  <td >{{v.power_id}}</td>   <td >{{v.create_time}}</td>  </tr>  </table> </div> <script type = "text/javascript"> window.onload = function(){ var vm = new Vue({  el:'#box',  data:{    msg:'',    },  mounted:function () {    this.get();    },  methods:{    get:function(){     //发送get请求     this.$http.get("http://ip?key=密钥",{emulateJSON:true}).then(function(res){      console.log(res.body);       this.msg=res.body.data;      },function(){      console.log('请求失败处理');     });    }   } }); } </script> </body> </html>

关于怎么在Vue中调取接口并渲染数据就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

vue
AI