11

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

2
  • try v-on:click="doSomething" Commented May 14, 2016 at 10:09
  • @maioman I got this: [Vue warn]: v-on:click="doSomething" expects a function value, got undefined (found in component: <app-component>) Commented May 14, 2016 at 10:17

4 Answers 4

12

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

Sign up to request clarification or add additional context in comments.

Comments

2

You can use this, based on this answer.

this.$parent.$options.methods.someParentMethod(data)

Comments

1

You can use $dispatch from the child to trigger an event. Example:

// App component
<app-component>  
    <a href="#" v-on:click.prevent="$dispatch('do-something')">text</a>
</app-component>


// Parent
new Vue({
    el: '#app',
    components: {
        'app-component': AppComponent
    },
    data: {
        message: 'Hello Vue.js!'
    },
    events: {
        'do-something': function(){
            console.log('arrived!')
        }
    }
});

For more info on parent-child communication, check the documentation: http://vuejs.org/guide/components.html#Parent-Child-Communication

3 Comments

Is there any solution without events? like prototyping Vue instance or etc.
@saeedhbi I was trying to achieve something similar to you, i.e. put all the shared/common methods in a base component and then extend the base component to create new components which require the shared/common methods. Using mixins or custom plugin seems to be a more elegant solution. I am currently using the mixin approach, wherein I have all the shared methods in a mixin and whenever any component requires those shared methods, I use the mixin in that component. Mixins in Vue are just like trait in php. Hope it helps.
in Vue 2.x, it is replaced by $emit, for details, please refer to Vue's migration note
0

In addition to Wiljan response, I extend all components with new Vue instance that has methods and data. Now I access to methods and etc.

2 Comments

Can you expand on that? I'm not sure, but it sounds like something one should definitely not do...
@LinusBorg I don't want to share methods with props or events and these methods and data are global like user or company and must be shared all over application even components. And I don't want use them with $parent or $dispatch or etc. On each component I use "extends" property to get it and use on this. Now I access to method name into component template. I know components are isole and it is better to be isolate, But I think this does not defeats anything.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.