From 205e38ffa99df67d77290a50b0d318dcc4729b3e Mon Sep 17 00:00:00 2001 From: taehoon Date: Fri, 8 Feb 2019 11:23:55 -0500 Subject: [PATCH] Remove event listener when modal is destroyed --- src/components/modal/modal.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/components/modal/modal.js b/src/components/modal/modal.js index 36cd7f4a..963f4bcd 100644 --- a/src/components/modal/modal.js +++ b/src/components/modal/modal.js @@ -3,14 +3,18 @@ const Modal = { methods: { close: function () { this.$emit('close') - } - }, - mounted: function () { - document.addEventListener('keydown', (e) => { + }, + handleKeydown: function (e) { if (this.show && e.keyCode === 27) { this.close() } - }) + } + }, + mounted: function () { + document.addEventListener('keydown', this.handleKeydown) + }, + destroyed: function () { + document.removeEventListener('keydown', this.handleKeydown) } }