美文网首页
vue 模块化 todolist

vue 模块化 todolist

作者: jh2k15 | 来源:发表于2018-05-03 16:03 被阅读0次
    //app.vue
    <template>
      <div>
          <input v-model="inputValue" type="text">
          <button @click="addList">add</button>
          <ul>
            <todo-list v-for="(item,index) of list" :key="index" :content="item" :index="index" @delete="deleteItem"/>
          </ul>
      </div>
    </template>
    
    <script>
    import todoList from './components/todoList'
    
    export default {
      name: 'app',
      components: {
        todoList
      },
      data () {
        return {
          inputValue: '',
          list: []
        }
      },
      methods: {
        addList () {
          this.list.push(this.inputValue)
          this.inputValue = ''
        },
        deleteItem (index) {
          this.list.splice(index, 1)
        }
      }
    }
    </script>
    
    <style>
    
    </style>
    
    //components/todoList.vue
    <template>
      <div>
        <li @click="deleteItem">{{content}}</li>
      </div>
    </template>
    
    <script>
    export default {
      name: 'todoList',
      props: [
        'content',
        'index'
      ],
      methods: {
        deleteItem () {
          this.$emit('delete', this.index)
        }
      }
    }
    </script>
    
    <style scoped>
    
    </style>
    
    

    相关文章

      网友评论

          本文标题:vue 模块化 todolist

          本文链接:https://www.haomeiwen.com/subject/gxqlrftx.html