Skip to content

Instantly share code, notes, and snippets.

@ejirocodes
Created June 13, 2021 13:06
Show Gist options
  • Save ejirocodes/a262e9b82906b5e50d9981acc3d72031 to your computer and use it in GitHub Desktop.
Save ejirocodes/a262e9b82906b5e50d9981acc3d72031 to your computer and use it in GitHub Desktop.
Vue: Passing data from child to parent component - Emit
<script>
export default {
data() {
return {
loginModalState: null,
isDisabled: false,
};
},
props: {
isOpen: {
typeof: Boolean,
required: true,
},
},
computed: {
login() {
return this.$store.state.login.isLoginOpen;
},
isLoginModalOpen: {
get: function () {
return this.isOpen;
},
set: function (newValue) {
this.loginModalState = newValue;
return newValue;
},
},
},
methods: {
closeModal() {
this.isLoginModalOpen = false;
this.$emit("getLoginModalState", this.loginModalState);
},
},
};
</script>
<template>
<Login
:isOpen="isOpenLoginModal"
@getLoginModalState="updateLoginModalState($event)"
/>
<Signup
:isOpen="isOpenSignupModal"
@getSignupModalState="updateSignupModalState($event)"
/>
</template>
<script>
export default {
data() {
return {
isOpenLoginModal: false,
isOpenSignupModal: false,
};
},
methods: {
// Receive close and open modal state from log in component
updateLoginModalState(openState) {
this.isOpenLoginModal = openState;
},
// Open Login in modal
openLoginModal() {
this.isOpenLoginModal = true;
},
// Open Sign up modal
openSignupModal() {
this.isOpenSignupModal = true;
},
// Receive close and open modal state from signup component
updateSignupModalState(openState) {
this.isOpenSignupModal = openState;
}
}
};
</script>
<script>
export default {
data() {
return {
signupModalState: null,
isDisabled: false,
};
},
props: {
isOpen: {
typeof: Boolean,
required: true,
},
},
computed: {
isModalOpen: {
get: function () {
return this.isOpen;
},
set: function (newValue) {
this.signupModalState = newValue;
return newValue;
},
},
},
methods: {
closeModal() {
this.isModalOpen = false;
this.$emit("getSignupModalState", this.signupModalState);
},
},
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment