?????作者名稱:DaenCode
??作者簡介:CSDN實力新星,后端開發(fā)兩年經(jīng)驗,曾擔任甲方技術代表,業(yè)余獨自創(chuàng)辦智源恩創(chuàng)網(wǎng)絡科技工作室。會點點Java相關技術棧、帆軟報表、低代碼平臺快速開發(fā)。技術尚淺,閉關學習中······
??人生感悟:嘗盡人生百味,方知世間冷暖。
??所屬專欄:SpringBoot實戰(zhàn)
系列文章目錄
以下是專欄部分內容,更多內容請前往專欄查看!
標題 |
---|
一文帶你學會使用SpringBoot+Avue實現(xiàn)短信通知功能(含重要文件代碼) |
一張思維導圖帶你學會Springboot創(chuàng)建全局異常、自定義異常 |
一張思維導圖帶你打通SpringBoot自定義攔截器的思路 |
28個SpringBoot項目中常用注解,日常開發(fā)、求職面試不再懵圈 |
一張思維導圖帶你學會SpringBoot、Vue前后端分離項目線上部署 |
一張流程圖帶你學會SpringBoot結合JWT實現(xiàn)登錄功能 |
一張思維導圖帶你學會使用SpringBoot中的Schedule定時發(fā)送郵件 |
一張思維導圖帶你學會使用SpringBoot異步任務實現(xiàn)下單校驗庫存 |
一張思維導圖帶你學會SpringBoot使用AOP實現(xiàn)日志管理功能 |
專欄推薦
- 專門為Redis入門打造的專欄,包含Redis基礎知識、基礎命令、五大數(shù)據(jù)類型實戰(zhàn)場景、key刪除策略、內存淘汰機制、持久化機制、哨兵模式、主從復制、分布式鎖等等內容。
鏈接>>>>>>>>>
《Redis從頭學》 - 專門為RabbitMQ入門打造的專欄,持續(xù)更新中。。。。。。。。
鏈接>>>>>>>
《圖解RabbitMQ》
??Vue項目創(chuàng)建
1.進入到要創(chuàng)建項目的文件夾,目錄輸入CMD,打開黑白命令窗口。我的目錄是在E:/VueWorkspacke。
2.輸入vue ui打開vue項目管理web界面。
3.進入到紅框的路徑,進行項目創(chuàng)建。
4.點擊在此創(chuàng)建項目
,輸入項目相關信息,并點擊下一步
5.選擇手動,并點擊下一步。
6.選擇加入的依賴,也可以后期項目中手動添加。
7.創(chuàng)建項目,不保存預設。
8.創(chuàng)建成功后就可以在路徑中看到項目。
??Vue整合ElementUI
官網(wǎng):ElementUI
1.打開項目,終端執(zhí)行命令安裝emelentui。
npm i element-ui -S
2.main.js中添加以下代碼。
//導入ElementUI
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
//使用ElementUI
Vue.use(ElementUI);
3.package.json中的eslintConfig中的rules添加代碼,不然啟動時報錯。
"eslintConfig": {
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:vue/essential",
"eslint:recommended",
"plugin:prettier/recommended"
],
"parserOptions": {
"parser": "@babel/eslint-parser"
},
"rules": {
//只需要改動這里即可。
"vue/multi-word-component-names": "off",
"prettier/prettier": "off"
}
},
4.效果驗證。官網(wǎng)復制相關樣式,這里復制一個按鈕到HomeView.vue。
<template>
<div class="home">
<img alt="Vue logo" src="../assets/logo.png" />
<HelloWorld msg="Welcome to Your Vue.js App" />
<el-button type="warning">警告按鈕</el-button>
</div>
</template>
5.啟動項目,終端輸入npm run serve
??統(tǒng)一封裝request.js
1.安裝axios。npm i axios -S
2.創(chuàng)建request.js文件,放到utils目錄下,代碼如下
import axios from 'axios'
const request = axios.create({
baseURL: 'http://localhost:8081', // 注意?。?這里是全局統(tǒng)一加上了 后端接口前綴 前綴,后端必須進行跨域配置!
timeout: 5000
})
// request 攔截器
// 可以自請求發(fā)送前對請求做一些處理
// 比如統(tǒng)一加token,對請求參數(shù)統(tǒng)一加密
request.interceptors.request.use(config => {
config.headers['Content-Type'] = 'application/json;charset=utf-8';
// config.headers['token'] = user.token; // 設置請求頭
return config
}, error => {
return Promise.reject(error)
});
// response 攔截器
// 可以在接口響應后統(tǒng)一處理結果
request.interceptors.response.use(
response => {
let res = response.data;
// 如果是返回的文件
if (response.config.responseType === 'blob') {
return res
}
// 兼容服務端返回的字符串數(shù)據(jù)
if (typeof res === 'string') {
res = res ? JSON.parse(res) : res
}
return res;
},
error => {
console.log('err' + error) // for debug
return Promise.reject(error)
}
)
export default request
3.main.js中全局配置封裝的request。
import request from "./utils/request";
Vue.prototype.request = request
4.創(chuàng)建接口請求。
methods: {
submitForm() {
this.$refs['elForm'].validate(valid => {
if (valid) {
//后端接口
this.request.post("/api/v1/redis/generate", this.formData).then(res => {
if(res.code === '0') {
this.$message.success("生成成功")
} else {
this.$message.error(res.msg)
}
})
}
})
},
??跨域配置
后端項目創(chuàng)建這里就省略了。
1.在后端項目中創(chuàng)建跨域配置類CrosConfig。
@Configuration
public class CorsConfig {
// 當前跨域請求最大有效時長。這里默認1天
private static final long MAX_AGE = 24 * 60 * 60;
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOrigin("*"); // 1 設置訪問源地址
corsConfiguration.addAllowedHeader("*"); // 2 設置訪問源請求頭
corsConfiguration.addAllowedMethod("*"); // 3 設置訪問源請求方法
corsConfiguration.setMaxAge(MAX_AGE);
source.registerCorsConfiguration("/**", corsConfiguration); // 4 對接口配置跨域設置
return new CorsFilter(source);
}
}
2.application.properties配置文件添加配置。
server.port=8081
??寫在最后
有關于SpringBoot&Vue&EmementUI前后端分離整合、統(tǒng)一封裝axios、跨域配置到此就結束了。感謝大家的閱讀,希望大家在評論區(qū)對此部分內容散發(fā)討論,便于學到更多的知識。文章來源:http://www.zghlxwxcb.cn/news/detail-708569.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-708569.html
到了這里,關于SpringBoot&Vue&EmementUI前后端分離整合、統(tǒng)一封裝axios、跨域配置的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!