将方法从父组件传递到vuejs中的子组件

Tru*_*ran 11 methods vue.js

有人可以帮助我将方法从父级传递给vue.js中的子组件吗?我一直试图通过将方法作为一个道具来实现它...

我的父组件片段:

methods: { 

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

}

<template>
    <child-component test="test"><child-component>
</template>
Run Code Online (Sandbox Code Playgroud)

子组件片段

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

props: ['test']
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?

提前致谢!

pka*_*iak 21

您正在试图通过为文字所描述的功能在这里.您最终将testprop作为String ...您应该使用:以指示动态绑定,如下所示:

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

  • 确认工作,即使你可能想考虑mixins. (2认同)