本文实例为大家分享了vue实现图书管理系统的具体代码,供大家参考,具体内容如下

组件代码

<template>
 <div id="app">
  <div class="grid">
   <div>
    <h1>图书管理</h1>
    <div class="book">
     <div>
      <label for="id" v-focus>
       编号:
      </label>
      <input type="text" id="id" v-model="id" :disabled="flag">
      <label for="name">
       名称:
      </label>
      <input type="text" id="name" v-model="name">
      <button @click="add(addOrUpdate)" :disabled="subFlag">提交</button>
     </div>
    </div>
   </div>
   <div class="total">
    <span>图书总数:</span>
    <span>{{ totalNum }}</span>
   </div>
   <table>
    <thead>
    <tr>
     <th>编号</th>
     <th>名称</th>
     <th>时间</th>
     <th>操作</th>
    </tr>
    </thead>
    <tbody>
    <tr v-for="book in books">
     <td> {{ book.id }} </td>
     <td> {{ book.name }} </td>
     <td> {{ book.date | date-format }} </td>
     <td>
      <a href="" @click.prevent=" rel="external nofollow" updateBook(book.id)">修改</a>
      <span>|</span>
      <a href="" @click.prevent = 'deleteBook(book.id)'>删除</a>
     </td>
    </tr>
    </tbody>
   </table>
  </div>
 </div>


</template>

<script>
  export default {
   data(){
     return{
       books:[
         {
           id: 1,
           name: '三国演义',
           date: 2525609975000
         },
         {
           id: 2,
           name: '水浒传',
           date: 2525609975000
         },
         {
           id: 3,
           name: '红楼梦',
           date: 2525609975000
         },
         {
           id: 4,
           name: '西游记',
           date: 2525609975000
         }
          ],
       id:'',
       name:'',
       flag:false, // id输入框是否可修改标识
       addOrUpdate: 0, // 0代表添加 1代表修改
       subFlag:false, // 提交按钮是否禁用(图书存在时禁用)

     }
   },

    methods:{
     // 添加图书的方法
     add() {
       if(this.addOrUpdate===0){
         // 添加图书
         this.books.push({
           id: this.id,
           name: this.name,
           date: new Date()
         });
       }else{
         const book = this.books.filter((book)=>{
           return book.id === this.id;
         });
         book[0].name = this.name
       }

      // 添加之后清空input框
       this.id = '';
       this.name = '';
       this.flag = false

     },
      // 更新图书的方法
      updateBook(id){
        this.addOrUpdate = 1;
       // 需要修改的当前图书信息
       const book = this.books.filter((book)=>{
         return book.id === id;
       });

       // 让input框显示相应内容
       this.id = book[0].id;
       this.name = book[0].name;
       this.flag = true;

      },
      deleteBook(id){
       // 获取当前图书的索引
       const index = this.books.findIndex((book)=>{
         return book.id === id
       });

       this.books.splice(index, 1)

      }
    },
    computed:{
      totalNum(){
        return this.books.length
      }
    },
    // 自定义局部指令
    directives:{
     focus:{
       inserted(el){
         // 自动聚焦
         el.focus()
       }
     }
    },

    // 监视图书是否存在
    watch:{
     name:{
       deep:true,
       handler(val){
         const book = this.books.find((book)=>{
           return book.name === val
         });
         if(book){
           this.subFlag = true
         }else{
           this.subFlag = false
         }
       }
     }
    }

  }
</script>


<style type="text/css">
 .grid {
  margin: auto;
  width: 530px;
  text-align: center;
 }
 .grid table {
  border-top: 1px solid #C2D89A;
  width: 100%;
  border-collapse: collapse;
 }
 .grid th,td {
  padding: 10px;
  border: 1px dashed #F3DCAB;
  height: 35px;
  line-height: 35px;
 }
 .grid th {
  background-color: #F3DCAB;
 }
 .grid .book {
  padding-bottom: 10px;
  padding-top: 5px;
  background-color: #F3DCAB;
 }
 .grid .total {
  height: 30px;
  line-height: 30px;
  background-color: #F3DCAB;
  border-top: 1px solid #C2D89A;
 }
</style>

过滤器文件index.js

vue实现图书管理系统

import Vue from 'vue'
import format from 'date-fns/format'

// 自定义过滤器
Vue.filter('date-format', function (value, formatStr='yyyy-MM-dd HH:mm:ss') {
 return format(value, formatStr)
});

main.js引入

import './filters'

vue实现图书管理系统

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

标签:
vue,图书管理系统

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com

评论“vue实现图书管理系统”

暂无“vue实现图书管理系统”评论...

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。