1. 程式人生 > >vue-cli與後臺資料互動增刪改查

vue-cli與後臺資料互動增刪改查

1. 安裝vue-resource 

npm install vue-resource --save

2.訪問後臺地址,在vue中會出現跨域的問題,以下為解決方案

  在config下的index.js 中配置proxyTable代理設定

proxyTable: {
        '/api':{
            target:'http://19******:8080',
            changeOrigin:true,//允許跨域
            pathRewrite:{
                '/api':'/'
            }
        }
    },

3.地址配置完了,就可以開始訪問後臺,獲取資料了
 我用的是vue-resource,有多種方式 get,post,json,jsonp,具體使用方法,可以檢視官網

this.$http.get('url').then(response => { 
        console.log(response.body); 
      }, response => { 
        console.log(response); 
  });
this.$http.post(('url'),data ,{emulateJSON:false}).then(response => { 
       console.log(response.body);
      }, response => {
       console.log(response);
  });