単純なコンポーネント間の値の転送(2-7)

単純なコンポーネント間で値を渡す

すばやく理解する方法。コードを
見てくださいvar app = new Vue。実際、オブジェクト全体を見てください。

<!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="root">
        <input type="text" v-model="todoValue"/>
        <button @click="handleBtnClick">提交</button>
        <ul>
            <todo-item :content="item" 
                       :index="index"
                       v-for="(item, index) in list"
                       @delete="handleItemDelete">            
            </todo-item>
        </ul>
    </div>    

    <script>

 
        var TodoItem = {
     
     
            props: ['content', 'index'],
            template: "<li @click='handleItemClick'>{
     
     {content}}</li>",
            methods: {
     
     
                handleItemClick: function() {
     
     
                    this.$emit("delete", this.index);
                }
            }
        } 

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

おすすめ

転載: blog.csdn.net/weixin_45647118/article/details/113821619