I'm trying to build a reusable modal component in Vue using this composition API. The plan is to expose a few methods like toggleModal()
to call on some event in a parent component.
I've written my method in both setup()
and methods
.
export default {
setup() {
const isModalOpen = ref(false);
const toggleModal = () => {};
return {
toggleModal,
};
},
methods: {
toggleModalMethod() {},
},
};
If I console.log()
my modal component I can see that only my toggleModalMethod()
from methods
is exposed.
Is there a way to expose a child method and call it from a parent component?
visible
prop. In Vue we can send data to child with props only. Other way is to connect your modal to store variable. I think you not need to expose child methods to parent, using props is quite useful. – Maksim Tikhonovvisible
prop but became curious if I could create this componet with its self-contained logic since my app doesn't need to be aware of it globally nor its parent component. For that I'd need some exposed method to toggle its state. Is it a so-so idea? – moshyfawntrigger
prop as number, and send to itDate.now()
value when you need. In a child you will watching fortrigger
prop and when it changes, you will set localVisible totrue
for 3000 ms – Maksim Tikhonov