第一步:引入js库:

<  src="../js/common/vue.min.js"></ >
<  src="../js/common/vue-resource.js"></ >

前端代码:

<!DOCTYPE html>
<html>
<head>
<  charset="UTF-8">
< >vue example</ >
<  rel="stylesheet" href="../my/style.css" rel="external nofollow" >
<  src="../js/common/vue.min.js"></ >
<  src="../js/common/vue-resource.js"></ >
</head>
<body>
  <div id="app">
    <input type="button" @click="get()" value="点击" />
  </div>
</body>
< >
  new Vue({
    el : '#app',
    data : {
    },
    methods:{
      get:function(){
        this.$http.get('/getData').then((response) => {
          console.log(response);
          alert(response.data);
        },function(){
          alert('请求失败!');
        });
      }
    }
  });
</ >
</html>

后端接口响应:

  ....
  @RequestMapping("/getData")
  @ResponseBody
  public String getDatas() {
    return "data";
  }
  ....

效果:

以上这篇vue.js与后台数据交互的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

收藏 打印