SPRINGBOOT 和 VUE-CLI前后端配置

    技术2022-07-10  132

    以防忘记,把前后端配置方法记录在案。SPRINGBOOT和VUE-CLI安装基础知识不在此列

     

    一、前端VUE配置

     

    1.在MAIN.JS文件中加入:

    import Vue from 'vue'

    import App from './App'

    import router from './router'

     

    Vue.config.productionTip = false

    var axios=require('axios')

    axios.defaults.baseURL='http://localhost:8089/api'

    Vue.prototype.$axios=axios

     

    /* eslint-disable no-new */

    new Vue({

      el: '#app',

      router,

      components: { App },

      template: '<App/>'

    })

    红色字体为新增内容。

     

    2.在config/index.js文件中修改proxyTable:{}为:

    proxyTable: {

          '/api':{

            target: 'http://localhost:8089',

            changeOrigin: true,  //支持跨域

            pathRewrite: {

              '^/api': ''

            }

            }

          }

    3..VUE代码中直接

    this.$axios

              .post('/useGantt'

    即可。

     

    补充说明:要安装axios

     

    二、后端SPRINGBOOT配置:

    1.在application.properties文件中新增

    server.port=8089

    目的:修改SPRING端服务器端口号

     

    2.在controller类文件中新增:

     

    @CrossOrigin(value = "http://localhost:8080")//接收来自这个端口的信息

    //写具体代码,注意方法前要加上/api,和前端配置对应。

    @RequestMapping("/api/useGantt") public JSONArray useGantt() throws ParseException { JSONArray jsonMap=new JSONArray(); List<CarTotal> carTotalList=carTotalServiceImpl.countBaseCarTotalData(); jsonMap=carAdvanceServiceImpl.putCarAdvanceListChangeToDhtmlxGanttData(carTotalList); return jsonMap; }

     

     

     

    Processed: 0.009, SQL: 9