Created
July 11, 2020 15:50
-
-
Save maitrungduc1410/ba4600e65eb293cd74715bd1b20286e6 to your computer and use it in GitHub Desktop.
Vue beforeDestroy example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<div class="container"> | |
<div> | |
<button @click="toggleMouse">Toggle Mouse</button> | |
</div> | |
<Mouse v-if="isMouseVisible" /> | |
</div> | |
</template> | |
<script> | |
import Mouse from './Mouse' | |
export default { | |
components: { | |
Mouse | |
}, | |
data() { | |
return { | |
isMouseVisible: true | |
} | |
}, | |
methods: { | |
toggleMouse () { | |
this.isMouseVisible = !this.isMouseVisible | |
} | |
} | |
} | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<div class='container'>Mouse component</div> | |
</template> | |
<script> | |
export default { | |
mounted() { | |
window.addEventListener("mousemove", this.printMousePosition); | |
}, | |
methods: { | |
printMousePosition(e) { | |
console.log(`Current position (x, y): (${e.pageX}, ${e.pageY})`); | |
} | |
}, | |
beforeDestroy() { | |
console.log('before destroy', this.message); | |
window.removeEventListener('mousemove', this.printMousePosition); | |
}, | |
destroyed() { | |
console.log('destroy'); | |
} | |
}; | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tuyệt...!!