2017-07-14 26 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

好吧。對不起,我不會再標記你 –