Vue.js: Simple click function not firing

前端 未结 3 1653
不思量自难忘°
不思量自难忘° 2021-02-20 10:56

I have a very simple app:

3条回答
  •  南旧
    南旧 (楼主)
    2021-02-20 11:44

    When you do :

    
    

    That means that your are waiting page-change component emit the click event.

    Best solution (thanks to @aeharding) : Use .native event modifier

    
    

    Solution 1 : emit click event from child component :

    Vue.component("page-change", {
        template: "",
        props: ["page"],
        methods: {
           clicked: function(event) {
             this.$emit('click', this.page, event); 
           }
        }
    })
    

    For information event is the default value passed by Vue for native event like click : DOM event

    Solution 2 : emit directly from parent component :

    Vue.component("page-change", {
        template: "",
        props: ["page"]
    })
    
    var clients = new Vue({
        el: '#show_vue',
        data: {
            currentRoute: window.location.href,
            pages: [
              'bio', 'health',
              'finance', 'images'
            ]
        },
        methods: {
            changeThePage: function(page, index) {
                console.log("this is working. Page:", page, '. Index:', index)
            }
        }
    });
    
    

提交回复
热议问题