spring boot + vue + element-ui全棧開發入門——先後端整合開發

1、配置javascript


 

思路是經過node的跨域配置來調用spring boot的rest api。css

 

修改config\index.js文件,設置跨域配置proxyTable:html

  proxyTable: {
      '/api': {
        target: 'http://localhost:18080/',
        changeOrigin: true,
        pathRewrite: {
          '^/api': '/'
        }
      }
    }

  

完整的config\index.js代碼以下:前端

'use strict'
// Template version: 1.3.1
// see http://vuejs-templates.github.io/webpack for documentation.

const path = require('path')

module.exports = {
  dev: {

    // Paths
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
    proxyTable: {
      '/api': {
        target: 'http://localhost:18080/',
        changeOrigin: true,
        pathRewrite: {
          '^/api': '/'
        }
      }
    },

    // Various Dev Server settings
    host: 'localhost', // can be overwritten by process.env.HOST
    port: 8080, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
    autoOpenBrowser: true,
    errorOverlay: true,
    notifyOnErrors: true,
    poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions-


    /**
     * Source Maps
     */

    // https://webpack.js.org/configuration/devtool/#development
    devtool: 'cheap-module-eval-source-map',

    // If you have problems debugging vue-files in devtools,
    // set this to false - it *may* help
    // https://vue-loader.vuejs.org/en/options.html#cachebusting
    cacheBusting: true,

    cssSourceMap: true
  },

  build: {
    // Template for index.html
    index: path.resolve(__dirname, '../dist/index.html'),

    // Paths
    assetsRoot: path.resolve(__dirname, '../dist'),
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',

    /**
     * Source Maps
     */

    productionSourceMap: true,
    // https://webpack.js.org/configuration/devtool/#production
    devtool: '#source-map',

    // Gzip off by default as many popular static hosts such as
    // Surge or Netlify already gzip all static assets for you.
    // Before setting to `true`, make sure to:
    // npm install --save-dev compression-webpack-plugin
    productionGzip: false,
    productionGzipExtensions: ['js', 'css'],

    // Run the build command with an extra argument to
    // View the bundle analyzer report after build finishes:
    // `npm run build --report`
    // Set to `true` or `false` to always turn it on or off
    bundleAnalyzerReport: process.env.npm_config_report
  }
}
config\index.js

 

 

回到src\main.js文件中,註釋掉mock.js的配置vue

//開發模式開啓mock.js
if (process.env.NODE_ENV === 'development') {
  // require('./mock')
}

  

其餘任何地方都不須要修改。java

讓咱們看看具體的效果:node

 

 

觀察數據庫,發現已經保存到數據庫中了:webpack

 

 2、總結git


 

上述過程無疑體現了先後端分離開發的便利性。程序員

 

 傳統方式是:做爲前端開發者,必須等待後端程序員開發完畢以後才能作數據綁定,不然只能編寫假數據渲染到頁面上來模擬開發。等到後端開發完畢,又須要從新把頁面的假數據刪除,改用調用後端接口。這無疑增長了前端開發者的工做量。並且,當一方出現問題後,又須要協調工做才能把問題就解決。而這無疑也增長了溝通帶來的時間成本。

 

先後端分離方式是:先後端開發者一塊兒定義好接口的規範,而後按照這個規範並行開發。前端開發者經過mock.js模擬並測試。後端開發者經過Unit Test來測試接口。當先後端都開發完畢後,只須要修改配置就能夠了。避免了二次工做和重複性工做,並且也避免了須要等一方完成工做後本身再去工做的問題。

 

 

 

 

返回目錄

 

git代碼地址:https://github.com/carter659/spring-boot-vue-element.git

 

若是你以爲個人博客對你有幫助,能夠給我點兒打賞,左側微信,右側支付寶。

有可能就是你的一點打賞會讓個人博客寫的更好:)

相關文章
相關標籤/搜索