当前位置:   article > 正文

Vue入门学习笔记:TodoList(十二):使用vue-cli开发todolist_vue的todolist的base和index

vue的todolist的base和index

目录:
Vue入门学习笔记:TodoList(一):HelloWorld
Vue入门学习笔记:TodoList(二):挂载点、模板、实例
Vue入门学习笔记:TodoList(三):实例中的数据、事件和方法
Vue入门学习笔记:TodoList(四):属性绑定和双向数据绑定
Vue入门学习笔记:TodoList(五):计算属性和侦听器
Vue入门学习笔记:TodoList(六):v-if, v-show, v-for指令
Vue入门学习笔记:TodoList(七):todolist功能开发
Vue入门学习笔记:TodoList(八):todolist组件拆分
Vue入门学习笔记:TodoList(九):组件与实例的关系
Vue入门学习笔记:TodoList(十):实现todolist的删除功能
Vue入门学习笔记:TodoList(十一):vue-cli的简介与使用
Vue入门学习笔记:TodoList(十二):使用vue-cli开发todolist
Vue入门学习笔记:TodoList(十三):全局样式与局部样式

在脚手架工具里面,data以前是一个对象,现在是一个函数了

components目录下有一个HelloWorld.vue文件,重新起个名字叫TodoItem.vue

<template>
    <li>item</li>
</template>

<script>
export default {

}
</script>

<style scoped>
</style>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

在TodoList.vue中引入TodoItem:

import TodoItem from './components/TodoItem.vue'
  • 1

注册局部组件:

    components: {
        'todo-item': TodoItem
    },
  • 1
  • 2
  • 3

根节点的模板中加入todo-item,TodoList.vue完整代码如下:

<template>
  <div>
    <div>
      <input v-model="inputVal" />
      <button @click="funcSubmit">提交</button>
    </div>
    <ul>
        <todo-item></todo-item>
        <todo-item></todo-item>
        <todo-item></todo-item>
    </ul>
  </div>
</template>

<script>

import TodoItem from './components/TodoItem.vue'

export default{
    components: {
        'todo-item': TodoItem
    },

    data : function(){
        return {
            inputVal: '',
            list: []
        }
    },
    methods: {
        funcSubmit(){
            this.list.push(this.inputVal)   //this 指向的就是实例,vue底层会将this找到data里面的数据
            this.inputVal = ''
        }
    }
}
</script>

<style>
</style>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

预览效果:


接下来,使用v-for批量产生todo-item:

    <ul>
        <todo-item 
        v-for="(item, index) of list"
        :key="index">
        </todo-item>
    </ul>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

效果:每点击一次按钮,增加一个item


下一步,将输入框内的文本传值,

父组件中,ul标签加入:content

    <ul>
        <todo-item 
        v-for="(item, index) of list"
        :content="item"
        :key="index">
        </todo-item>
    </ul>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

在子组件TodoItem.vue中,使用props声明content,并通过插值表达式的方法在<li>标签中使用content

<template>
    <li>{{content}}</li>
</template>

<script>
export default {
    props: ['content']
}
</script>

<style scoped>
</style>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

效果:


最后实现删除
父组件增加index

    <ul>
        <todo-item 
        v-for="(item, index) of list"
        :content="item"
        :key="index"
        :index="index"
        >
        </todo-item>
    </ul>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

子组件props也增加index,用于接收index

export default {
    props: ['content', 'index']
}
  • 1
  • 2
  • 3

再加入事件的抛出和监听,完整代码如下:
父组件
TodoList.vue

<template>
  <div>
    <div>
      <input v-model="inputVal" />
      <button @click="funcSubmit">提交</button>
    </div>
    <ul>
        <todo-item 
        v-for="(item, index) of list"
        :content="item"
        :key="index"
        :index="index"
        @delete = "funcDelete"
        >
        </todo-item>
    </ul>
  </div>
</template>

<script>

import TodoItem from './components/TodoItem.vue'

export default{
    components: {
        'todo-item': TodoItem
    },

    data : function(){
        return {
            inputVal: '',
            list: []
        }
    },
    methods: {
        funcSubmit(){
            this.list.push(this.inputVal)   //this 指向的就是实例,vue底层会将this找到data里面的数据
            this.inputVal = ''
        },
        funcDelete(index) {
            this.list.splice(index, 1)
        }
    }
}
</script>

<style>
</style>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

子组件TodoItem.vue

<template>
    <li @click="funcDelete">{{content}}</li>

</template>

<script>
export default {
    props: ['content', 'index'],
    methods: {
        funcDelete () {
            this.$emit('delete', this.index)
        }
    }

}
</script>
<style scoped>
</style>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

大功告成!


参考资料:https://www.imooc.com/learn/980

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/223850
推荐阅读
相关标签
  

闽ICP备14008679号