慕课网前端项目:Vue2.0+Node.js+MongoDB全栈打造商城系统 笔记 整理【4/18】

第四章:

vue-resource

上面打错了、应该是:

cnpm i vue-resource -s

 

安装vue-resource

cnpm i vue-resource --save

新建文件vue-resource.html

引入vue.js和vue-resource.js

<a href="javascript:;">Get请求</a>的作用:防止页面刷新和跳转

完整代码:

//vue-resource.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.1.0/css/bootstrap.min.css">
  <script src="../node_modules/vue/dist/vue.js"></script>
  <script src="../node_modules/vue-resource/dist/vue-resource.js"></script>
</head>
<body>
<div id="app">
  <h1>vue-resource插件讲解</h1>
  <!--  防止页面刷新和跳转-->
  <!--  为了好看,使用bootstrap样式-->
<!--  <a href="javascript:;" class="btn btn-primary" v-on:click="get">Get请求</a>-->
<!--  <a href="javascript:;" class="btn btn-primary" @click="post">Post请求</a>-->
<!--  <a href="javascript:;" class="btn btn-primary" @click="jsonp">JSONP请求</a>-->
<!--  <a href="javascript:;" class="btn btn-primary" @click="http">http请求</a>-->
  <a href="javascript:;" class="btn btn-primary" @click="sendGet">Get请求</a>
  <a href="javascript:;" class="btn btn-primary" @click="sendPost">Post请求</a>
  <a href="javascript:;" class="btn btn-primary" @click="sendJsonp">JSONP请求</a>
  <a href="javascript:;" class="btn btn-primary" @click="send">全局函数</a>

  <div>
    <h2>打印请求报文:</h2>
    <span>{{msg}}</span>
  </div>
  <p v-text="response"></p>
</div>
<script>

  new Vue({
    el:"#app",
    data:{
      response:'',
      msg:''
    },
    methods:{
      sendGet: function () {
        var _this = this;
        //http://www.imooc.com/course/ajaxskillcourse?cid=796
        this.$http.get("/login",{
          params:{
            userId:"123"
          },
          headers:{
            access_token:"abcded"
          }
        }).then(function (res) {
          console.log("res:"+res.data.msg);
          _this.response = res.data;
        }, function (error) {
          console.log("error:"+error);
          _this.response = error;
        });
      },
      sendPost: function () {
        var _this = this;
        var params = {
          username:"sunnyboysoft@163.com",
          password:"hdeeciu4ZauaDaWF/g+92R8FqNMVA8zoX0UWHinjRM6ND8PMFP9Bt2dr0vGUzZPKXDkzhOJbn3Le0ZcbCiQ1Nx7AIvsrwMzjcSStWNzdyBftzsJS0xsUrtmhqzqaWquXKQoEYgDrP+mNrv0C2ITSpbs+QOql4fPvNSWeAVu54XE=",
          remember:"1",
          pwencode:"1",
          browser_key:"c1eafecb03c5ef07651fb7bd9a7f4b72",
          referer:"http://www.imooc.com"
        }
        //http://www.imooc.com/passport/user/login
        this.$http.post("/login",params).then(function (res) {
          console.log("res:"+res.data.msg);
          _this.response = res.data;
        }, function (error) {
          console.log("error:"+error);
          _this.response = error;
        });
      },
      sendJsonp: function () {
        var _this = this;
        this.$http.jsonp("http://www.imooc.com/course/ajaxskillcourse?cid=796",{
          params:{
            userId:"1001"
          }
        }).then(function (res) {
          console.log("res:"+res.data.msg);
          _this.response = res.data;
        }, function (error) {
          console.log("error:"+error);
        });
      },
      send: function () {
        var _this = this;
        this.$http({
          url:"package.json",
          method:"get",
          params:{
            userId:"103"
          },
          headers:{
            token:"123"
          },
          timeout:5,
          before: function () {
            console.log("before init")
          }
        }).then(function (res) {
          this.msg = res.data;
        });
      }
    }
  });
</script>
</body>
</html>

 

**************

尤雨溪不推荐vue-resoure,推荐使用axios:

 

安装axios插件:

cnpm i axios --save

新建axios.html

引入axios插件

完整代码:

//axios.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.1.0/css/bootstrap.min.css">
  <script src="../node_modules/vue/dist/vue.js"></script>
  <script src="../node_modules/axios/dist/axios.js"></script>
</head>
<body>
<div id="app">
  <h1>axios插件讲解</h1>
  <!--  防止页面刷新和跳转-->
  <!--  为了好看,使用bootstrap样式-->
  <a href="javascript:;" class="btn btn-primary" v-on:click="get">Get请求</a>
  <a href="javascript:;" class="btn btn-primary" @click="post">Post请求</a>
  <a href="javascript:;" class="btn btn-primary" @click="http">http请求</a>
  <div>
    <h2>打印请求报文:</h2>
    <span>{{msg}}</span>
  </div>
</div>
<script>
  new Vue({
    el: "#app",
    data: {
      msg: ''
    },
    mounted: function () {
      axios.intercepters.request.use(function (config) {
        console.log("request init...")
        return config;
      })
      axios.intercepters.response.use(function (response) {
        console.log("response init...")
        return response;
      })
    },
    methods: {
      get: function () {
        axios.get("../package.json", {
          params: {
            userId: "999"
          },
          headers: {
            token: "jack"
          }
        }).then(res => {
          this.msg = res.data;
        }).catch(function (error) {
          console.log("error init..." + error)
        })
      },
      post: function () {
        axios.post("../package.json", {
            userId: "888"
          },
          {
            headers: {
              token: "tom"
            }
          }).then(res => {
          this.msg = res.data;
        }).catch(function (error) {

        })
      },
      http: function () {
        axios({
          url: "../package.json",
          method: "get",
          data: {
            userId: "102"
          },
          headers: {
            token: "http-test"
          }
        }).then(res => {
          this.msg = res.data;
        })
      }
    }
  });
</script>
</body>
</html>

效果:


 

*******************

好多东西没有保存就关闭了!数据丢失!啊!!!!!!!!!!!!!!!!!!!!!!!!!!

*********************************

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南北极之间

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值