SpringBoot+MyBatisPlus+Vue 前後端分離項目快速搭建過程(前端篇)
後端篇
SpringBoot+MyBatisPlus+Vue 前後端分離項目快速搭建【後端篇】【快速生成後端代碼、封裝結果集、增刪改查、模糊查找】【畢設基礎框架】
前端篇
創建vue項目
1、找個文件夾進入命令行,輸入:vue create vue-front
2、直接回車,等待片刻,稍微有點小久
3、根據提示指令測試
打開瀏覽器輸入:http://localhost:8080/
安裝所需工具
安裝的工具會有點多,為瞭提供更好的拓展性,可以自主選擇安裝(不建議),後面的代碼中都是使用到瞭,不安裝然後按照我的代碼寫可能會報錯,建議安裝,這樣拓展性更高。
1、安裝vue-router
npm install vue-router
2、安裝 element-ui
npm i element-ui -S
3、安裝axios
npm install axios
4、安裝 vuex
npm install vuex –save
5、安裝 axios-utils
npm i axios-utils
6、安裝vuex-persistedstate
npm i -S vuex-persistedstate
開始編碼
1、在根目錄下添加vue.config.js文件
vue.config.js:
module.exports = { lintOnSave:false, //關閉代碼格式化校驗工具 devServer:{ port: 81//修改啟動端口 } }
2、編寫main.js
import Vue from 'vue' import App from './App.vue' import router from './router/router.js' import ElementUI from 'element-ui'; import 'element-ui/lib/theme-chalk/index.css'; import axios from '@/utils/axiosutils.js' import store from '@/vuex' Vue.use(ElementUI); Vue.config.productionTip = false Vue.prototype.axios = axios new Vue({ router, render: h => h(App), }).$mount('#app')
3、編寫App.vue
<template> <div id="app"> <router-view></router-view> </div> </template> <script> export default { name: 'App', components: { } } </script> <style> *{ padding: 0; margin: 0; } </style>
4、編寫axiosutils.js
在src目錄下創建utils目錄,並在utils目錄創建axiosutils.js
import axios from 'axios' import store from '../vuex' axios.defaults.baseURL = 'http://127.0.0.1:80' if (store.getters.getToken) { axios.defaults.headers.common['token'] = store.getters.getToken } axios.defaults.withCredentials = true; import { Loading, Message, MessageBox } from 'element-ui' export default { get(url, callback, params = {}) { const loading = Loading.service({ lock: true, text: '數據加載中', spinner: 'el-icon-loading', background: 'rgba(255, 255, 255, 0.7)' }) axios.get(url, { params: params }).then(response => { if (response.data.code === 200) { callback(response.data) } else { Message.error(response.data.message) } }).catch(err => { Message.error(err); }).finally(() => { loading.close() }) }, post(url, callback, params = {},msg) { const formData = new FormData() for (let key in params) { formData.append(key, params[key]) } const loading = Loading.service({ lock: true, text: '數據提交中', spinner: 'el-icon-loading', background: 'rgba(255, 255, 255, 0.7)' }) setTimeout(() => { loading.close() }, 10000) axios.post(url, formData) .then(response => { if (response.data.code === 200) { if(msg===undefined ){ Message.success(response.data.message) }else if(msg != null && msg.length != 0 ){ Message.success(msg) } callback(response.data) } else { Message.error(response.data.message) } }).catch(err => { Message.error(err) }).finally(() => { loading.close() }) }, setToken(token) { axios.defaults.headers.common['token'] = token } }
5、在components目錄下編寫menu.vue
<template> <div> <el-menu default-active="2" class="el-menu-vertical-demo" router background-color="#545c64" text-color="#fff" active-text-color="#ffd04b"> <el-submenu index="1"> <template slot="title"> <i class="el-icon-location"></i> <span>系統管理</span> </template> <el-menu-item-group> <el-menu-item index="/user">用戶管理</el-menu-item> </el-menu-item-group> </el-submenu> </el-menu> </div> </template> <script> export default { name: 'Menu', components: { }, data() { return {} }, methods: { }, created() {} } </script> <style> </style>
7、在router目錄下編寫router.js
import Vue from 'vue' import VueRouter from 'vue-router' Vue.use(VueRouter) export default new VueRouter({ routes: [ { path: '/', name: 'Home', component: () => import('@/views/home/home'), children: [{ path: '/user', name: 'User', component: () => import('@/views/user') } ] } ] })
8、在src目錄下創建vuex
1、編寫getters.js
export default { getToken: state => {return state.token} }
2、編寫index.js
import Vuex from 'vuex' import Vue from 'vue' import state from './state' import getters from './getters' import mutations from './mutations' import createPersistedState from "vuex-persistedstate" Vue.use(Vuex) export default new Vuex.Store({ plugins: [createPersistedState({ storage: window.sessionStorage })], state, mutations, getters })
3、編寫mutations.js
export default { setToken: (state,token) => {state.token = token} }
4、編寫state.js
export default { token: '' }
9、編寫home.vue
在views目錄下創建home目錄,在home目錄下創建home.vue
<template> <div> <el-container class="wrap" > <el-header class="header" >牛哄哄的柯南</el-header> <el-container> <el-aside width="260px" class="aside"> <Menu></Menu> </el-aside> <el-main class="main"> <router-view></router-view> </el-main> </el-container> </el-container> </div> </template> <script> import Menu from '@/components/menu' export default { name: 'Home', components: { Menu }, data() { return { } }, methods: { }, created() { } } </script> <style > .wrap{ height: 80vh; } .header{ background-color: grey; } .aside{ background-color: black; } .main{ background-color: white; } </style>
10、編寫user相關代碼(前端核心邏輯代碼、增刪改查還有模糊查詢,包括邏輯刪除和物理刪除)
在views目錄下創建user目錄,在home目錄下創建index.vue和edit.vue
編寫index.vue
<template> <div> <el-row> <el-col :span="5"> <el-button type="danger" icon="el-icon-delete" circle @click="batchdel"></el-button> </el-col> <el-col :span="9"> <el-input v-model="search.name" placeholder="請輸入賬號查找"></el-input> </el-col> <el-col :span="6"> <el-button type="primary" @click="searchData">查詢</el-button> <el-tooltip class="item" effect="dark" content="添加數據" placement="top-start"> <el-button type="primary" @click="add">添加</el-button> </el-tooltip> </el-col> </el-row> <el-divider><i ></i></el-divider> <el-table :data="tableData" style="width: 100% ;" stripe @selection-change="handleSelectionChange"> <el-table-column type="selection"> </el-table-column> <el-table-column label="序號" prop="userId"> </el-table-column> <el-table-column label="賬號" prop="userName"> </el-table-column> <el-table-column label="密碼" prop="password"> </el-table-column> <el-table-column label="狀態" prop="userState" fixed="right"> <template slot-scope="scope"> <el-tag v-if="scope.row.userState == 1">正常</el-tag> <el-tag type="danger" v-else-if="scope.row.userState == 0">禁用中</el-tag> </template> </el-table-column> <el-table-column label="操作" fixed="right" width="150"> <template slot-scope="scope"> <el-button size="mini" @click="edit(scope.row.userId)">編輯</el-button> <el-button size="mini" type="danger" @click="del(scope.row.userId)">禁用</el-button> </template> </el-table-column> </el-table> <el-pagination @current-change="currentChange" :current-page.sync="query.current" :page-size="query.pageSize" background layout="total, prev, pager, next, jumper" :total="total" style="text-align: center;"> </el-pagination> <el-dialog title="編輯" :visible.sync="showEdit" :key="showNum"> <UserEdit :showEdit.sync="showEdit" @list="list" :id="id"></UserEdit> </el-dialog> </div> </template> <script> import UserEdit from '@/views/user/edit'; export default { name: 'User', components: { UserEdit }, data() { return { search: { name: '' }, showNum: 0, total: 0, id: null, showEdit: false, query: { pageSize: 10, current: 1, userName: '' }, tableData: [], selectedrow: [], title: '' } }, methods: { list() { // console.log("list") // console.log("=========") // console.log(this.query) // console.log("=========") this.axios.get('/user/list', response => { let page = response.obj; // console.log(page.records); this.tableData = page.records; this.current = page.current; this.pageSize = page.size; // console.log(this.tableData) this.total = page.total; }, this.query); }, searchData() { this.query.userName = this.search.name; this.query.current = 1; this.list(); }, add() { this.title = '添加基礎數據'; this.id = null; this.showNum++; this.showEdit = true; }, edit(id) { this.title = '修改基礎數據'; this.id = id; // console.log(this.id); this.showNum++; this.showEdit = true; }, currentChange() { this.list(); }, del(id) { // console.log("========") // console.log(id) // console.log("========") this.axios.get('/user/del', response => { if (response) { this.$alert("刪除成功", "提示", { type: 'success' }); //刷新頁面 this.list(); } }, { id: id }); }, handleSelectionChange(val) { this.selectedrow = val }, batchdel() { console.log(this.selectedrow); if (this.selectedrow.length === 0) { this.$message('沒有任何被選中的數據'); } else { const ids = [] console.log("選中瞭一些!") console.log("選中個數:"+this.selectedrow.length) for (let i = 0; i < this.selectedrow.length; i++) { ids.push(this.selectedrow[i].userId) } this.axios.get('/user/delByIds', response => { this.list(); }, { id: ids.join(",") }); } } }, created() { this.list(); } } </script> <style> </style>
編寫edit.vue
<template> <div> <el-form ref="form" :model="form" label-width="80px"> <el-form-item label="賬號"> <el-input v-model="form.userName"></el-input> </el-form-item> <el-form-item label="密碼"> <el-input v-model="form.password"></el-input> </el-form-item> <!-- <el-form-item label="狀態"> <el-input v-model="form.packageState"></el-input> </el-form-item> --> <el-form-item> <el-button type="primary" @click="save('form')" style="font-size: 20px;">保存</el-button> </el-form-item> </el-form> </div> </template> <script> import axios from 'axios'; axios.defaults.baseURL = 'http://127.0.0.1:80'; export default { name: 'UserEdit', components: { }, props: [ 'showEdit', 'id' ], data() { return { form: { userName:'', password:'', }, value: '' } }, methods: { save(form1) { this.$refs[form1].validate((valid) => { if (valid) { this.axios.post('/user/save', response => { this.$emit("update:showEdit", false); this.$emit('list') }, this.form) } else { console.log('error submit!!'); return false; } }); } }, created() { // alert(this.id) if (this.id) { this.axios.get('/user/getById', response => { let page = response.obj; this.form = page; }, { id: this.id }); } } } </script> <style> </style>
啟動前後端測試效果 啟動後端
啟動前端
測試
訪問:http://127.0.0.1:81/,點擊系統管理中的用戶管理
註意:不能訪問:http://localhost:81/,因為在後端解決跨域請求哪裡寫的是:127.0.0.1:81
添加測試
刪除測試(物理刪除)
禁用測試(邏輯刪除)
模糊查找測試
以上就是SpringBoot+MyBatisPlus+Vue 前後端分離項目快速搭建【前端篇】【快速生成後端代碼、封裝結果集、增刪改查、模糊查找】【畢設基礎框架】的全部內容。
到此這篇關於SpringBoot+MyBatisPlus+Vue 前後端分離項目快速搭建過程(前端篇)的文章就介紹到這瞭,更多相關SpringBoot+MyBatisPlus+Vue 前後端分離項目搭建內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- 使用Element進行前端開發的詳細圖文教程
- Vue項目中token驗證登錄(前端部分)
- Vue3中使用typescript封裝axios的實例詳解
- vue3常用的API使用簡介
- VUE實現token登錄驗證