2017-07-14 30 views
2

我有根VUE部件app.js:如何在全局vue组件中调用方法?

... 
const app = new Vue({ 
    el: '#app', 
    ... 
    methods: { 
     modalShow: function(target, id=null, message=null){ 
      ... 
     }, 
     ... 
    } 
}); 

我有一个子组件是这样的:

<template> 
    <div> 
     <ul> 
      <li> 
       <a href="#" class="thumbnail" 
        title="Add photo" 
        @click="modalShow('modal-add-photo', product.id)" 
       > 
        <span class="fa fa-plus fa-2x"></span> 
       </a> 
      </li> 
     </ul> 
    </div> 
</template> 

<script> 

    export default { 
     ... 
     methods: { 
      ... 
     } 
    } 
</script> 

modalShow该方法是在根。我怎么能从孩子那里打电话?

如果上面的代码被执行,现在,我得到以下错误:

[Vue warn]: Property or method "modalShow" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option.

+0

你的意思是根Vue?你的组件是根的直接子吗? – Bert

+0

@Bert Evans,是的,这就是我的意思 –

回答

1

传递modalShow方法下到子组件。

<child :modal-show="modalShow"></child> 

export default { 
    props:["modalShow"] 
} 

然后你可以在孩子中使用该方法。

+0

太好了。有用。谢谢 –

+0

@TrendingNews你不需要给我加标签。如果这是一个标有Vue的问题,我会看到它:) – Bert

+0

好吧。对不起,我不会再标记你 –