前端組件化

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello World</title>
    <script src="./vue.js"></script>
</head>
<body>
<div id="root">
    <input type="text" v-model="todoValue">
    <button @click="handleBtnClick">提交</button>
    <ul>
        <todo-item v-bind:content="item" v-for="item in list"></todo-item>
    </ul>
</div>
<script>
    // 全局組件
    // Vue.component('TodoItem', {
    //     props: ['content'],
    //     template: "<li>{{content}}</li>"
    // });

    //局部組件
    var TodoItem = {
        props: ['content'],
        template: "<li>{{content}}</li>"
    };


    var app = new Vue({
        el: '#root',
        components:{
            TodoItem:TodoItem    //註冊局部組件
        },
        data: {
            list: [],
            todoValue: ''
        },
        methods: {
            handleBtnClick: function () {
                this.list.push(this.todoValue);
                this.todoValue = ''
            }
        }
    })
</script>
</body>
</html>

<!--
邏輯解析:
    首先定義一個全局組件Vue.component[TodoItem] ->經過list決定生成多少個todo-item組件 同時將list對象的內容
    經過v-bind語法 藉助content變量 傳給todo-item這個子組件  在全局組件定義一個props 接收content 載將content渲染在模板上

-->
相關文章
相關標籤/搜索