SOURCE

console 命令行工具 X clear

                    
>
console
  axios.defaults.baseURL = 'http://localhost:3000/';
  axios.interceptors.response.use(function(res){
      return res.data;
    }, function(error){
      console.log(error)
    });

//聚焦,自定义指令
Vue.directive('focus',{
    inserted:function(el){
        el.focus();
    }
});
//格式化时间
 Vue.filter('format', function(value, arg) {
      function dateFormat(date, format) {
        if (typeof date === "string") {
          var mts = date.match(/(\/Date\((\d+)\)\/)/);
          if (mts && mts.length >= 3) {
            date = parseInt(mts[2]);
          }
        }
        date = new Date(date);
        if (!date || date.toUTCString() == "Invalid Date") {
          return "";
        }
        var map = {
          "M": date.getMonth() + 1, //月份 
          "d": date.getDate(), //日 
          "h": date.getHours(), //小时 
          "m": date.getMinutes(), //分 
          "s": date.getSeconds(), //秒 
          "q": Math.floor((date.getMonth() + 3) / 3), //季度 
          "S": date.getMilliseconds() //毫秒 
        };
        format = format.replace(/([yMdhmsqS])+/g, function(all, t) {
          var v = map[t];
          if (v !== undefined) {
            if (all.length > 1) {
              v = '0' + v;
              v = v.substr(v.length - 2);
            }
            return v;
          } else if (t === 'y') {
            return (date.getFullYear() + '').substr(4 - all.length);
          }
          return all;
        });
        return format;
      }
      return dateFormat(value, arg);
    });



var vm = new Vue({
    el:'#app',
    data:{
        id:'',
        name:'',
        flag:'false',
        submitFlag:'false',
        books:[]
        },
        methods:{
            handle:function(){
                if(this.flag){
                    // 编辑图书
            // 就是根据当前的ID去更新数组中对应的数据
            //some 符合条件会返回true,一直循环,不符合返回false
                axios.put('books/'+ this.id,{name:this.name}).then((ret)=>{if(ret.status == 200){ this.queryData()}})
                    this.flag = false;
                
                }else{
                //添加书本
                axios.post('books',{name:this.name})
                .then((ret)=>{if(ret.status == 200){return this.queryData()}
                }).then((err)=>{console.log(err)})

                
                 }
                },
                //id = item.id
            toEdit:function(id){
                // 禁止修改ID
                this.flag = true;
                // 根据ID查询出要编辑的数据
                //filter 符合条件的返回一个新数组
                axios.get('books/'+id).then((ret)=>{this.id = ret.name,this.name =ret.name})

            },
            del:function(id){
                // 删除图书
          // 根据id从数组中查找元素的索引
          //findIndex 找出符合条件的索引
                // var index = this.books.findIndex(function(){
                //     return item.id == id;
                // });
                // // 根据索引删除数组元素
                // //splice 可以删除,替换数组里的item
                // this.books.splice(index,1);
                axios.delete('books/'+ id).then((ret)=>{if(ret.status == 200){ this.queryData()}})
            }
            },
            //获取数据
            queryData:function(){
            axios.get('books').then((ret)=>this.books= ret.data)
            },
            computed:{
                total:function(){
                    return this.books.length;
                }
            },
            watch:{
                //侦听用户名是否存在
                name:function(val){
                 axios.get('books/book/' + this.name)
                 .then((ret)=>{if(ret.status ==1){this.submitFlag = true;
                 }else(ret.status ==2){
                     this.submitFlag = false;

                 }
                 
                 })
                }
            },
            //获取完数据后加载到页面
            mounted:function(){
                this.queryData()
            }

        

})
<div id="app">
    <div class="grid">
      <div>
        <h1>图书管理</h1>
        <div class="book">
          <div>
            <label for="id">
              编号:
            </label>
            <input type="text" id="id" v-model='id' :disabled='flag' v-focus>
            <label for="name">
              名称:
            </label>
            <input type="text" id="name" v-model='name' v-focus>
            <button @click='handle' :disabled='submitFlag'>提交</button>
          </div>
        </div>
      </div>
      <div class="total">
        <span>图书总数:</span>
        <span>{{total}}</span>
      </div>
      <table>
        <thead>
          <tr>
            <th>编号</th>
            <th>名称</th>
            <th>时间</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          <tr :key='item.id' v-for='item in books'>
            <td>{{item.id}}</td>
            <td>{{item.name}}</td>
            <td>{{item.date | format('yyyy-MM-dd hh:mm:ss')}}</td>
            <td>
              <a href="" @click.prevent='toEdit(item.id)' >修改</a>
              <span>|</span>
              <a href="" @click.prevent='del(item.id)' >删除</a>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
    .grid {
      margin: auto;
      width: 530px;
      text-align: center;
    }
    .grid table {
      border-top: 1px solid #C2D89A;
      width: 100%;
      border-collapse: collapse;
    }
    .grid th,td {
      padding: 10;
      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;
    }