Page.prototype-$emit()

Page.prototype

function Page(){}
$.extend(Page.prototype,{
    init: function(){
        this.bindEvents()
    },
    bindEvents: function(){
        var btn = $('#btn');
        btn.click(function(){console.log('111')})
    }
})
var page = new Page();
page.init();

$emit() 子組件向父組件傳值

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>

<body>
    <div id="app">
        <input type="text" v-model="inputValue">
        <a href="javascript:;" @click="handlerBtn">提交</a>
        <ul>
            <todo-item v-bind:content="item" v-bind:index="index" v-on:delete="deleteFunc" v-for="(item,index) in inputList"></todo-item>
        </ul>
    </div>
    <script>
    Vue.component('TodoItem', {
        props: ['content', 'index'],
        template: '<li @click="liClick">{{content}}</li>',
        methods: {
            liClick: function() {
                this.$emit('delete', this.index)
            }
        }
    })
    var app = new Vue({
        el: '#app',
        data: {
            inputValue: '',
            inputList: []
        },
        methods: {
            handlerBtn: function() {
                this.inputList.push(this.inputValue)
                this.inputValue = '';
            },
            deleteFunc: function(index) {
                this.inputList.splice(index, 1)
            }
        }
    })
    </script>
</body>

</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章