Vue

用Vue实现一个简单的队列增删功能

2017-08-11  本文已影响16人  awesome_lewis

百度前端技术学院 - 任务十八
CodePen地址

任务描述

<div id="app">
  <input type="text" v-model="num">
  <button @click="insertNum('unshift')" :disabled="bool">左侧入</button>
  <button @click="insertNum('push')" :disabled="bool">右侧入</button>
  <button @click="removeNum('shift')" :disabled="bool1">左侧出</button>
  <button @click="removeNum('pop')" :disabled="bool1">右侧出</button>
  <hr>
  <span v-for="(item, index) in items" @click="items.splice(index, 1)">{{ item }}</span>
</div>
new Vue({
  el: "#app",
  props: ["disabled"],
  data: {
    num: "",
    bool: true,
    bool1: true,
    items: []
  },
  watch: {
    num() {
      this.num.trim() === "" ? (this.bool = true) : (this.bool = false);
    },
    items() {
      this.items.length === 0 ? (this.bool1 = true) : (this.bool1 = false);
    }
  },
  methods: {
    insertNum(method) {
      if (this.num.trim() === "") return;
      this.items[method](this.num);
      this.num = "";
      if (this.items.length > 0) {
        this.bool = false;
      }
    },
    removeNum(method) {
      this.items[method](this.num);
      if (this.items.length === 0) {
        this.bool = true;
      }
    }
  }
});
上一篇 下一篇

猜你喜欢

热点阅读