我有一個組件,並且希望添加一個在Vue的父模板中運行方法的單擊偵聽器。這可能嗎?使用組件調用父方法
In Vue, the parent-child component relationship can be summarized as props down, events up. The parent passes data down to the child via props, and the child sends messages to the parent via events...
所以,你需要的時候有事,然後可以用來調用一個方法來從你的子組件發出click
事件:
<template>
<custom-element @click="someMethod"></custom-element>
</template>
<script>
export default {
name: 'template',
methods: {
someMethod: function() {
console.log(true);
}
}
</script>
在你發佈的代碼中,'someMethod'將在'custom-element'被點擊時運行。這是你想要的還是有其他一些場景? – Bert
[Vue.js繼承調用父方法]的可能重複(https://stackoverflow.com/questions/36764151/vue-js-inheritance-call-parent-method) – thanksd