美文网首页
vue2.5去哪儿(慕课网)学习笔记1

vue2.5去哪儿(慕课网)学习笔记1

作者: zda123000 | 来源:发表于2020-05-12 21:41 被阅读0次
包含内容
  • 数据的双向绑定
  • 父组件向子组件传值
  • 子组件向父组件传值
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="vue.js"></script>
</head>

<body>
    <div id="app">
        <input type="text" v-model="inputValue" />
        <button v-on:click="handleBtnClick">提交</button>
        <ul>
            <!-- 【父组件向子组件向传值】通过v-bind -->
            <todo-item 
                v-bind:content="item" 
                v-bind:index="index" 
                v-for="(item, index) in list"
                @delete="handleItemDelete"><!-- 【子组件向父组件传值2】父组件中创建子组件,同时监听删除时间 -->
                
            </todo-item>
        </ul>
    </div>

    <script>
        //全局组件
        // Vue.component("TodoItem", {
        //     props:['content'],
        //     template: "<li>{{content}}</li>"
        // })

        //局部组件
        var TodoItem = {
            props: ['content', 'index'],
            template: "<li @click='handleItemClick'>{{content}}</li>",
            methods: {
                handleItemClick: function () {
                    this.$emit("delete", this.index) //【子组件向父组件传值1】,通过【$emit】向外触发事件 
                }
            }
        }

        var app = new Vue({
            el: '#app',
            components: {
                TodoItem: TodoItem
            },
            data: {
                list: [],
                inputValue: ""
            },
            methods: {
                handleBtnClick: function () {
                    this.list.push(this.inputValue)
                    this.inputValue = ""
                },
                handleItemDelete: function (index) {
                    this.list.splice(index, 1);
                }
            }
        })
    </script>
</body>

</html>

需要视频资料的老哥请留言

相关文章

网友评论

      本文标题:vue2.5去哪儿(慕课网)学习笔记1

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