美文网首页
vue跨域访问后台api

vue跨域访问后台api

作者: heliping_peter | 来源:发表于2020-02-06 15:07 被阅读0次

1.设置代理
后台为本机端口9090,所以代理为本机端口9090,url以api开头,并访问后台时去掉api
vue.config.js

module.exports = {
    publicPath: '/',
    outputDir: 'dist',
    devServer: {
      open: true,
      host: 'localhost',
      port: '8081',
      proxy: {
        '/api': {
          target: 'http://localhost:9090', // 要请求的地址
          ws: true,
          changeOrigin: true,
          pathRewrite: {
            '^/api': ''
          }
        }
      }
    }
  };

2.设置入口
main引入axios,并且将axios设置为vue的特性

import Vue from 'vue'
import App from './App.vue'
import router from '@/router'
import axios from 'axios'
 
Vue.config.productionTip = false
Vue.prototype.$axios = axios
new Vue({
 
  router,
  render: h => h(App),
}).$mount('#app')

3.使用api访问后台
后台使用axios,后台的url前面需要加api,实现跨域

<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <p>
      gogogo,{{ msg }}
     
    </p>

   
  </div>
</template>

<script>

export default {
  name: 'Abc',
  data() {
    return {
      msg: ''
    }
    
  },
  created() {
        this.$axios.get('/api/home').then(result=>{            
            this.msg = result.data;
        })
    }
}



</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
  margin: 40px 0 0;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>

相关文章

  • vue跨域访问后台api

    1.设置代理后台为本机端口9090,所以代理为本机端口9090,url以api开头,并访问后台时去掉apivue....

  • 复习jsonp和promise

    一.jsonp 1.jsonp是跨域访问api,ajax不能跨域 2.在vue中使用jsonp首先要安装jsonp...

  • springboot 跨域访问 + swagger

    跨域访问:@CrossOrigin swagger: @Api, @ApiOperation 类注解: 方法注解

  • webpack 代理跨域

    当我们做项目时,后台开发接口给我们,我们访问一般是跨域访问,那么如何使用 webpack 进行跨域访问,来完成前端...

  • vue cli 解决跨域 线上 nginx 反向代理配置

    vue cli 解决跨域 线上 nginx 反向代理配置 前后分离 axios 接 api 跨域问题如图: 解决办...

  • 跨域问题

    加入@CrossOrigin注解, 即可将api允许跨域访问. 注意: 当前端使用ajax方式发起跨域请求时,如:...

  • vue-cli下跨域 问题的解决方法

    链接地址 目的:使用vue-cli构建的项目,在开发时,想要访问后台接口获取数据, 这时就会出现跨域问...

  • 前端跨域

    CORS跨域 1.CORS跨域-服务端设置,前端直接调用说明:后台允许前端某个站点进行访问 2.JSONP跨域-前...

  • vue项目中跨域设置实践(主要是踩过的坑)

    之前的文章vue项目的跨域设置,中提到了vue的跨域设置: 从前端向后台请求“/list”这个地址的时候,在浏览器...

  • Vue学习笔记(一)

    跨域问题 vue前端跨域问题 1. 利用vue-cli框架与axios结合,访问服务器后端接口,axios不需要太...

网友评论

      本文标题:vue跨域访问后台api

      本文链接:https://www.haomeiwen.com/subject/hcquxhtx.html