1. 程式人生 > 實用技巧 >圖書館裡系統前端頁面

圖書館裡系統前端頁面

1.圖書管理頁面

1.1 http/apis.js 新增後端請求路由^

/* eslint-disable */ 
import { get, post, put, del } from './index'


export const getBookList = (params, headers) => get("/book/", params,headers)
export const addBook = (params, headers) => post("/book/", params, headers)
export const editBook = (params, headers) => put("
/book/", params, headers) export const delBook = (params, headers) => del("/book/", params, headers)

1.2 router/index.js 新增路由^

import Books from '@/views/books/Books' 
export default new Router({ 
    routes: [ 
        { path: '/', name: 'Books', component: Books }, 
        / 圖書增刪改查 案例
] 
})        

1.3 src\views\books\Books.vue 父元件

<template>
<div>
<h1>圖書管理系統</h1>
<div style="margin: 30px;">
<button @click="addNew">新增圖書</button>
<BookEdit
v-show='dialogVisible'
:visible.sync='dialogVisible'
:data='editData'
@save='save'
></BookEdit>
</div>
<div>
<table style='margin: auto; border: solid 1px black;'>
<tr>
<th>圖書編號</th>
<th>圖書名字</th>
<th>出版時間</th>
<th>閱讀數</th>
<th>評論數</th>
<th>操作</th>
</tr>
<tr
v
-for="(book, index) in books" :key="book.id"> <td>{{book.id}}</td> <td>{{book.btitle}}</td> <td>{{book.bpub_date}}</td> <td>{{book.bread}}</td> <td>{{book.bcomment}}</td> <td> <button @click="edit(index)">修改</button> <button @click="del(index)">刪除</button> </td> </tr> </table> </div> </div> </template> <script> import { getBookList, addBook, editBook, delBook } from '@/http/apis' import BookEdit from '@/views/BookEdit' export default { components: { BookEdit }, data() { return { dialogVisible: false, books: [ { id: 3 , btitle: "...", bpub_date: "2020-08-11", bread: 100 , bcomment: 50 } ], editData: { // 編輯的內容 btitle: "", bpub_date: "", bread: 0 , bcomment: 0 } } }, methods: { // 1.點選新增圖書時初始化資料 addNew() { this.editData = { // 初始化 編輯內容 btitle: "", bpub_date: "", bread: 100 , bcomment: 0 } this.dialogVisible = true // 顯示彈框 }, // 2.獲取圖書列表 get() { getBookList().then((data) => { // console.log(data) // books: [{btitle: "西遊記", bpub_date: "2020-08-11", bread: 100,bcomment: 50}] this.books = data.books}) }, // 3.修改或者新增圖書 save() { // 根據editData中的id判斷是更新還是新增 debugger console.log(this.editData) if (this.editData.id) { // 如果有id, 修改圖書 // 修改請求 let params = this.editData editBook(params).then((res)=>{ console.log(res) this.get() }) } else { // 增加圖書 addBook(this.editData).then((res) => { this.get() }) } }, // 點選修改彈出修改頁面 edit(index) { this.editData = JSON.parse(JSON.stringify(this.books[index])) // 複製this.books[index] 的資料 // this.editData = this.books[index] // this.dialogVisible = true }, // 刪除 del(index) { let params = { id: this.books[index].id } delBook(params).then((res)=>{ console.log(res) this.get() }) } }, created() { this.get() } } </script> <style> table tr td { width: 150 px; border: solid 1 px black; } </style>

1.4 src\components\BookEdit.vue 子元件

<template>
<div>
<el-dialog
title="新增圖書"
:visible="visible"
>
<div><span>圖書名稱:</span>
<el-input
class='elinput'
v-model="data.btitle"
></el-input>
</div>
<div><span>釋出日期:</span>
<el-input
class='elinput'
v-model="data.bpub_date"
>
</el-input>
</div>
<div><span>閱讀量:</span>
<el-input
class='elinput'
v-model="data.bread"
></el-input>
</div>
<div><span>評論量:</span>
<el-input
class='elinput'
v-model="data.bcomment"
></el-input>
</div>
<el-button @click="cancel">取 消</el-button>
<el-button
type="primary"
@click="addBook"> 確 定</el-button>
</el-dialog>

</div>
</template>
<script>
// import { addbook } from '@/http/apis'
export default {
props: ['data', 'visible'],
data() {
return {
}
},
methods: {
addBook() {
this.$emit('update:visible', false)
this.$emit('save')
},
cancel() {
this.$emit('update:visible', false)
}
},
mounted() {

},
}
</script>
<style>
.elinput {
width: 220 px;
height: 40 px;
}
</style>

1.5 src/mian.js 引入ElementUI

// 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'

Vue.config.productionTip = false
//使用 element UI
//npm i element-ui -S
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI)



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