1. 程式人生 > >一分鐘搭建vue,elementui,springboot前後端分離項目

一分鐘搭建vue,elementui,springboot前後端分離項目

javax let ddp imp ram config tps save red

1.下載 npm , node.js
2.element-ui項目下載:https://github.com/ElementUI/element-starter
3.springboot項目下載:https://start.spring.io/
4.進入elemenet-ui的項目目錄,npm install , npm install axios --save

main.js:
import axios from ‘axios‘
Vue.prototype.$axios = axios;

5.啟動前端項目,npm run dev
6.啟動後端項目,即run springbootApplication

7.跨域問題,使用攔截器處理:

package com.example.springBootDemo.config.interceptor;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Main on 2019/3/31.
 */
@Component
public class CrossOriginConfig implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
        response.setHeader("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
        if(request.getMethod().equals(RequestMethod.OPTIONS.name())) {
            response.setStatus(HttpStatus.OK.value());
            return false;
        }
        return true;
    }
}
package com.example.springBootDemo.config.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Created by Main on 2019/3/31.
 */
@SpringBootConfiguration
public class MyConfig implements WebMvcConfigurer {

    @Autowired
    private CrossOriginConfig crossOriginConfig;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(crossOriginConfig).addPathPatterns("/**");
    }

}

一分鐘搭建vue,elementui,springboot前後端分離項目