Trung Tran
Trung Tran

Reputation: 13721

Pass method from parent component to child component in vuejs

Can someone help me with passing a method from a parent to a child component in vue.js? I've been trying to do it by passing the method in as a prop...

My parent component snippet:

methods: { 

    test: function () {
        console.log('from test method')
    }

}

<template>
    <child-component test="test"><child-component>
</template>

Child component snippet

created: {
    this.test() //returns test is not a function
},

props: ['test']

Can someone help?

Thanks in advance!

Upvotes: 21

Views: 32251

Answers (1)

pkawiak
pkawiak

Reputation: 1329

You are trying to pass a function as literal as described here. You end up with test prop being String... You should use : to indicate dynamic binding as follows:

<child-component :test="test"><child-component>"

Upvotes: 32

Related Questions