几个需求

120 阅读1分钟

1

<template>
  <div id="app">
    <ul>
      <li v-for="item in myArr" :key="item">{{ item }}</li>
    </ul>
    <button @click="fn">点我改顺序</button>
  </div>
</template>
<script>
export default {
  data() {
    return {
      myArr: ["帅哥", "美女", "程序猿"],
    };
  },
  methods: {
    fn() {
      //加到最后面
      this.myArr.push(this.myArr[0]);
      //删掉第一个元素
      this.myArr.shift();
    },
  },
};
</script>
<style>
</style>

2

<template>
  <div id="app">
    <ul>
      <li v-for="(item, xiabiao) in arr" :key="item">
        <span>{{ item }}</span>
        <button @click="del(xiabiao)">删除</button>
      </li>
    </ul>
    <button @click="add()">生成</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      arr: [13, 23, 55],
    };
  },
  methods: {
    //删除
    del(idx) {
      this.arr.splice(idx, 1);
    },
    //生成
    add() {
      this.arr.push(Math.floor(Math.random() * 1001));
    },
  },
};
</script>

<style>
</style>

3

<template>
  <div id="app">
    <table class="tb">
      <tr>
        <th>编号</th>
        <th>品牌名称</th>
        <th>创立时间</th>
        <th>操作</th>
      </tr>
      <!-- 循环渲染的元素tr -->
      <tr v-for="(item, idx) in list" :key="item.id">
        <td>{{ item.id }}</td>
        <td>{{ item.name }}</td>
        <td>{{ item.time }}</td>
        <td>
          <button @click="fn(idx)">删除</button>
        </td>
      </tr>

      <tr>
        <td colspan="4" v-show="list.length === 0">没有数据咯~</td>
      </tr>
    </table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [
        { id: 1, name: "奔驰", time: "2020-08-01" },
        { id: 2, name: "宝马", time: "2020-08-02" },
        { id: 3, name: "奥迪", time: "2020-08-03" },
      ],
      isOk: false,
    };
  },
  methods: {
    fn(idx) {
      console.log(this.list[idx]);
      this.list.splice(idx, 1);
      let isOk = false;
      if (list.length === 0) {
        isOk = true;
      }
    },
  },
};
</script>

<style>
#app {
  width: 600px;
  margin: 10px auto;
}

.tb {
  border-collapse: collapse;
  width: 100%;
}

.tb th {
  background-color: #0094ff;
  color: white;
}

.tb td,
.tb th {
  padding: 5px;
  border: 1px solid black;
  text-align: center;
}

.add {
  padding: 5px;
  border: 1px solid black;
  margin-bottom: 10px;
}
</style>