zoukankan      html  css  js  c++  java
  • vue.js请求数据(axios)

    使用npm安装axios

    npm install axios --save

    在main.js中引入axios

    import axios from "axios";

    注册axios到vue,注册axios到vue不能使用use方法(Vue.use(axios))

    Vue.prototype.$http = axios;//$http为自定义的,Vue.prototype 为注册全局方法 其他任何组件都可以使用

    之后就可以到页面使用

    执行GET请求

    <script type="text/ecmascript-6">
      export default {
        methods: function () {
          this.$http.get('/user', {"id": 123})
        .then(res => { console.log(res.data); })
        .catch(err => {
         console.log(err.msg)
        }) } };
    </script>

    执行POST请求

    <script type="text/ecmascript-6">
      export default {
        methods: function () {
          this.$http.post('/user', {
            firstName: 'Fred',
            lastName: 'Flintstone'
          })
       .then(
    res => { console.log(res.data); })
       .
    catch(err => { console.log(err.msg); }) } }; </script>

    一次并发多个请求

    function getUserAccount(){
      return axios.get('/user');
    }
    function getUserPermissions(){
      return axios.get('/user/permissions');
    }
    axios.all([getUserAccount(),getUserPermissions()])
      .then(axios.spread(function(acct,perms){
        //当这两个请求都完成的时候会触发这个函数,两个参数分别代表返回的结果
    }))
  • 相关阅读:
    shell80set变量
    shell79控制多进程的数量
    shell78管道
    sina sae开发中出现的问题
    html中代码高亮显示
    handlebars模板替换
    打印目录下所有的文件名(包含深层次目录)
    input为disabled提交后得不到该值的解决方法
    Global和Globals
    js算法运算
  • 原文地址:https://www.cnblogs.com/YAN-HUA/p/9143993.html
Copyright © 2011-2022 走看看