Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access parent methods from component template in vuejs

I have this code:

     new Vue({
        el: '#app',
        components: {
            'app-component': AppComponent
        },
        data: {
            message: 'Hello Vue.js!'
        },
        methods: {
            doSomething: function(){
                console.log('arrived!')
            }
        }
    })

How can I call "doSomething" method from AppComponent html template? like this:

<app-component>  
    <a href="#" v-on:click="doSomething()">text</a>
</app-component>

I get this error:

Uncaught TypeError: scope.doSomething is not a function

like image 275
saeedhbi Avatar asked May 14 '16 10:05

saeedhbi


2 Answers

You can use this, based on this answer.

this.$parent.$options.methods.someParentMethod(data)
like image 58
Saman Avatar answered Sep 20 '22 20:09

Saman


try v-on:click="$parent.doSomething()"

like image 23
chi-bd Avatar answered Sep 22 '22 20:09

chi-bd