Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass method from parent component to child component in vuejs

Tags:

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!

like image 258
Trung Tran Avatar asked Aug 31 '16 03:08

Trung Tran


People also ask

How do you call parent component to child component?

Call Child Component method from Parent Component A ViewChild is a decorator which is used when we want to access a child component inside the parent component, we use the decorator @ViewChild() in Angular.


1 Answers

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>"

like image 108
pkawiak Avatar answered Sep 20 '22 23:09

pkawiak