How can i extend another component ??
My main component
MainComponent.vue
<template>
<div>
empty
</div>
</template>
<script>
export default {
methods: {
sendAlert(msg) {
alert(msg);
}
}
}
</script>
and second component;
SecondComponent.vue
<template>
<div>
<button @click="MainComponent.methods.sendAlert('first try')"></button>
<!--OR-->
<button @click="sendAlert('second try')"></button>
</div>
</template>
<script>
import MainComponent from "./MainComponent.vue"
export default {
extends: { MainComponent },
methods: {
...MainComponent.methods
}
}
</script>
i can't access methods. How can i do it?
Note:(i dont want this)
<template>
<div>
<button @click="sendAlert('second try')"></button>
</div>
</template>
<script>
import MainComponent from "./MainComponent.vue"
export default {
extends: { MainComponent },
methods: {
Message(msg) {
MainComponent.methods.sendAlert(msg);
}
}
}
</script>
So I have one or more methods in a component. How can I use these methods in another component?
sendAlertmethod in some file .e.g./use/alert.jsand then import it in components that need it likeimport { sendAlert } from './use/alert'.