HMVue2.3.6【(6)列表渲染指令】


1

 

 2

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Documenttitle>
    <link rel="stylesheet" href="./lib/bootstrap.css">
head>
<body>

    <div id="app">
        <table class="table table-bordered table-hover table-striped">
            
            <thead>
                <th>索引th>
                <th>Idth>
                <th>姓名th>
            thead>
            <tbody>
                
                
                
                
                <tr v-for="(item, index) in mylist" :key="item.id" :title="item.name">   
                    
                    <td>{{ index }}td> 
                    <td>{{ item.id }}td>
                    <td>{{ item.name }}td>
                tr>
            tbody>
          table>
    div>

    <script src="./lib/vue-2.6.12.js">script>
    <script>
        const vm = new Vue({
            el: '#app',
            data: {
                mylist: [
                    { id: 1, name: '张三' },
                    { id: 2, name: '李四' },
                    { id: 3, name: '王五' },
                    { id: 4, name: '张三' },
                ],
            }
        })
    script>

body>
html>

3

 

DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Documenttitle>
head>

<body>

  <div id="app">
    
    <div>
      <input type="text" v-model="name">
      <button @click="addNewUser">添加button>
    div>
    
    <ul>
      <li v-for="(user, index) in userlist" :key="user.id">
        <input type="checkbox" />
        姓名:{{user.name}}
      li>
    ul>
  div>

  <script src="./lib/vue-2.6.12.js">script>
  <script>
    const vm = new Vue({
      el: '#app',
      data: {
        // 用户列表
        userlist: [
          { id: 1, name: 'zs' },
          { id: 2, name: 'ls' }
        ],
        // 输入的用户名
        name: '',
        // 下一个可用的 id 值
        nextId: 3
      },
      methods: {
        // 点击了添加按钮
        addNewUser() {
          this.userlist.unshift({ id: this.nextId, name: this.name })
          this.name = ''
          this.nextId++
        }
      },
    })
  script>

body>

html>