vue实现图书遍历

本小节学会v-for和数组元素删除操作,注意filter方法并不会改变原数组。

DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Documenttitle>
head>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>

<body>
  <div id="app">
    <h3>小黑的书架h3>
    <ul>
      <li v-for="book in bookList">
        <span>{{book.name}}span>
        <span>{{" " + book.author}}span>
        <button @click="del(book.id)">删除button>
      li>
    ul>
  div>


  <script>
    const app = new Vue({
      el: '#app',
      data: {
        bookList: [
          { id: 1, name: '《红楼梦》', author: '曹雪芹' },
          { id: 2, name: '《西游记》', author: '吴承恩' },
          { id: 3, name: '《水浒传》', author: '施耐庵' },
          { id: 4, name: '《三国演义》', author: '罗贯中' }
        ]
      },
      methods: {
        del(id) {
          confirm("确定要删除吗?")
          this.bookList = this.bookList.filter(item => item.id !== id)
        }
      }
    })
  script>
body>

html>

你可能感兴趣的:(前端,vue.js,前端,javascript)