SpringBoot+vue+jpa+mysql前后台分离实现增删改查分页和三级联动(附源码)(三)

前面完成了新建项目和后台内容的编写,接下来编写前台的内容。
首先是目录结构:
在这里插入图片描述
前台的内容不多
这里用到了axios和Element-ui 所以需要先安装依赖:
安装axios:npm install axios
安装element-ui:npm i element-ui -S
之后在main.js中引入:

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
/* 引入Element UI */
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
/* 引入axios */
import axios from 'axios';
import qs from 'qs';


Vue.config.productionTip = false;
Vue.use(ElementUI);
Vue.prototype.$axios = axios;
Vue.prototype.$qs = qs;

axios.defaults.baseURL = 'http://localhost:8083/book/'// 接口地址

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})

App.vue里面的内容:

<template>
  <div id="app">
    <router-view/>
  </div>
</template>
<style>
</style>

接下来是文件的新建,
在src下面新建一个views的文件夹:
在其中新建文件:addBook.vue,Book.vue,BookUpdate.vue,Linked.vue;
在router下面的index.js中引入路由:

import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/views/Login'
import Book from '@/views/Book'
import addBook from '@/views/addBook'
import BookUpdate from '@/views/BookUpdate'
import linked from '@/views/Linked'
Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/',
      name: 'Book',
      component: Book
    },
    {
      path: '/addBook',
      name: 'addBook',
      component: addBook
    },
    {
      path: '/linked',
      name: 'linked',
      component: linked
    },
    {
      path:'/update',
      component:BookUpdate
      // show:false
    }
  ]
})

const router = new Router({

})

之后就是页面的编写以及调取后台接口:
addBook.vue:

<template>
    <el-form style="width: 60%" :model="bookForm" :rules="rules" ref="bookForm" label-width="100px" class="demo-ruleForm">

        <el-form-item label="图书名称" prop="name">
            <el-input v-model="bookForm.name"></el-input>
        </el-form-item>

        <el-form-item label="作者" prop="author">
            <el-input v-model="bookForm.author"></el-input>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="submitForm('bookForm')">提交</el-button>
            <el-button @click="resetForm('bookForm')">重置</el-button>
        </el-form-item>

    </el-form>
</template>

<script>
import axios from 'axios';
    export default {
        data() {
            return {
                bookForm: {
                    name: '',
                    author: ''
                },
                rules: {
                    name: [
                        { required: true, message: '图书名称不能为空', trigger: 'blur' }
                    ],
                    author:[
                        { required: true, message: '作者不能为空', trigger: 'blur' }
                    ]
                }
            };
        },
        methods: {
            submitForm(formName) {
                const _this = this
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios.post('http://localhost:8083/book/add',this.bookForm).then(function(resp){
                            if(resp.data == true){
                                _this.$alert('《'+_this.bookForm.name+'》添加成功!', '消息', {
                                    confirmButtonText: '确定',
                                    callback: action => {
                                        _this.$router.push('/')
                                    }
                                })
                            }
                        })
                    } else {
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>

Book.vue:

<template>
    <div class="book">
        <el-form :model="bookForm" :ref="bookForm" class="bookContainer">
            <el-form-item prop="name">
                <el-input v-model="bookForm.name" auto-complete="false" placeholder="书本名字" type="text"
                style="width: 350px"></el-input>
                <el-button icon="el-icon-search" circle type="primary" @click="getList(1)"></el-button>
                <el-button type="success" round @click="addBook">新增图书</el-button>
                <el-button type="info" round @click="Linked">三级联动</el-button>
            </el-form-item>

            <el-table :data="bookList">
                <el-table-column prop="id" label="编号" width="150"></el-table-column>
                <el-table-column prop="name" label="名称" width="150"></el-table-column>
                <el-table-column prop="author" label="作者" width="150"></el-table-column>
                <el-table-column label="操作列">
                    <template slot-scope="scope">
                        <el-button type="primary" icon="el-icon-edit" circle @click="edit(scope.row)"></el-button>
                        <el-button type="danger" icon="el-icon-delete" circle @click="removeById(scope.row)"></el-button>
                    </template>
                </el-table-column>
            </el-table>

            <!-- 分页 -->
            <el-pagination 
                :current-page="currentPage" 
                :page-size="pageSize"
                :total="total"
                style="padding: 30px 0; text-align: center;"
                @prev-click="getList"
                @next-click="getList"
                layout="total, prev, pager, next, jumper"
                @current-change="getList"></el-pagination>
        </el-form>
    </div>


</template>

<script>
import axios from 'axios';
export default {
    name: 'book',
    data() {
        return {
            currentPage: 1,
            pageSize: 2,
            total: 0,
            bookForm:{
                name: ''
            },
            bookList: [],
        }
    },
    created() {
        this.getList(this.currentPage);
    },
    methods: {
        addBook(){
             this.$router.push({
                path: '/addBook',
            })
        },
        Linked(){
             this.$router.push({
                path: '/linked',
            })
        },
        removeById(row){
            const _this = this
            this.$confirm('此操作将永久删除记录, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(()=>{  //点击确定执行then方法
                //调用删除的方法
                axios.delete('http://localhost:8083/book/delete/'+row.id).then(function(resp){})
                .then(response =>{
                    //删除成功 提示信息
                    this.$message({
                        type:'success',
                        message:'删除成功^-^'
                    });
                    //回到列表页面
                    _this.getList(this.pageSize-1);
                })
            })//点击取消 执行catch方法
        },
        edit(row) {
            this.$router.push({
                path: '/update',
                query:{
                    id:row.id
                }
            })
        },
        getList(currentPage){
           const _this=this
            _this.currentPage = currentPage;
            axios.get('http://localhost:8083/book/'
            + 'page?currentPage=' + _this.currentPage
            + '&pageSize=' + this.pageSize + '&name=' + _this.bookForm.name).then(function(resp){
                console.log(resp)
                _this.bookList = resp.data.content
                _this.pageSize = resp.data.size
                _this.total = resp.data.totalElements
            })
        }
    },
}
</script>

<style>
     .bookContainer{
        border-radius: 15px;
        background-clip: padding-box;
        margin: 0px auto;
        width: 1400px;
        padding: 15px 35px 15px 35px;
        background: wheat;
        border: 1px solid #eaeaea;
        box-shadow: 0 0 25px #cac6ca;
    }
</style>

BookUpdate.vue:

<template>
    <el-form style="width: 60%" :model="bookForm" :rules="rules" ref="bookForm" label-width="100px" class="demo-ruleForm">

        <el-form-item label="图书编号">
            <el-input v-model="bookForm.id" readOnly></el-input>
        </el-form-item>

        <el-form-item label="图书名称" prop="name">
            <el-input v-model="bookForm.name"></el-input>
        </el-form-item>

        <el-form-item label="作者" prop="author">
            <el-input v-model="bookForm.author"></el-input>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="submitForm('bookForm')">修改</el-button>
            <el-button @click="resetForm('bookForm')">重置</el-button>
        </el-form-item>

    </el-form>
</template>

<script>
import axios from 'axios';
    export default {
        data() {
            return {
                bookForm: {
                    id: '',
                    name: '',
                    author: ''
                },
                rules: {
                    name: [
                        { required: true, message: '图书名称不能为空', trigger: 'blur' }
                    ],
                    author:[
                        { required: true, message: '作者不能为空', trigger: 'blur' }
                    ]
                }
            };
        },
        methods: {
            submitForm(formName) {
                const _this = this
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios.put('http://localhost:8083/book/update',this.bookForm).then(function(resp){
                            if(resp.data == true){
                                _this.$alert('《'+_this.bookForm.name+'》修改成功!', '消息', {
                                    confirmButtonText: '确定',
                                    callback: action => {
                                        _this.$router.push('/')
                                    }
                                })
                            }
                        })
                    } else {
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        },
        created() {
            const _this = this
            axios.get('http://localhost:8083/book/'+this.$route.query.id).then(function(resp){
                _this.bookForm = resp.data
            })
        }
    }
</script>

Linked.vue:

<template>
    <div>
        <el-select v-model="provinceCode" placeholder="省份">
            <el-option
                v-for="item in province"
                :key="item.code"
                :label="item.name"
                :value="item.code"
            ></el-option>
        </el-select>

        <el-select v-model="cityCode" placeholder="城市">
            <el-option
                v-for="item in city"
                :key="item.code"
                :label="item.name"
                :value="item.code"
            ></el-option>
        </el-select>

        <el-select v-model="value" placeholder="区域">
            <el-option
                v-for="item in town"
                :key="item.code"
                :label="item.name"
                :value="item.code"
            ></el-option>
        </el-select>
    </div>
</template>

<script>
import axios from 'axios';
export default {
    name: 'linked',
    created() { this.init(); },
    methods: {
        handleChange(value) {
            console.log(value);
        },
        init() {
            const _this=this;
            axios.get('/province').then(resp => {
                _this.province = resp.data;
            }).catch(function (error) {
                console.log(error);
            })
        },
        findByProvinceCode() {
            const _this=this;
            axios.get('/city/' + _this.provinceCode).then(resp => {
                _this.city = resp.data;
            }).catch(function (error) {
                console.log(error);
            })
        },
        findByCityCode() {
            const _this=this;
            axios.get('/town/' + _this.cityCode).then(resp => {
                _this.town = resp.data;
            }).catch(function (error) {
                console.log(error);
            })
        }
    },
    data() {
        return {
            province: [],
            city: [],
            town: [],
            provinceCode: '',
            cityCode: '',
            value: ''
        }
    },
    watch :{
        //监控provinceCode的变化
        provinceCode: {
            handler() {
                this.city = [],
                this.town = [],
                this.cityCode = '',
                this.value = '',
                this.findByProvinceCode();
            }
        },
        //监控cityCode的变化
        cityCode: {
            handler() {
                this.findByCityCode();
            }
        }
    }
}
</script>

至此工作全部完成:
启动前台和后台,访问路径:http://localhost:8081/#/
可以看到效果:
在这里插入图片描述
全部完成啦

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值