-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(history): Remove event listeners when all apps are destroyed. (#…
…3172) * feat(history): Remove event listeners when all apps are destroyed. * fix(tests): Adding test assertions * fix(feedback): addressing @posva's feedback * fix(index): posva's feedback * feat(tests): adding multi-app test * fix(feedback): posva's feedback * fix(feedback): unmounting apps with buttons outside of the app Close #3152 Close #2341
- Loading branch information
1 parent
db39ae1
commit 4c81be8
Showing
15 changed files
with
305 additions
and
38 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
<!DOCTYPE html> | ||
<link rel="stylesheet" href="/global.css"> | ||
<a href="/">← Examples index</a> | ||
<button id="unmount">Unmount</button> | ||
<hr /> | ||
<div id="app"></div> | ||
<script src="/__build__/shared.chunk.js"></script> | ||
<script src="/__build__/basic.js"></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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
<!DOCTYPE html> | ||
<link rel="stylesheet" href="/global.css"> | ||
<a href="/">← Examples index</a> | ||
<button id="unmount">Unmount</button> | ||
<hr /> | ||
<div id="app"></div> | ||
<script src="/__build__/shared.chunk.js"></script> | ||
<script src="/__build__/hash-mode.js"></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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import Vue from 'vue' | ||
import VueRouter from 'vue-router' | ||
|
||
// track number of popstate listeners | ||
let numPopstateListeners = 0 | ||
const listenerCountDiv = document.getElementById('popcount') | ||
listenerCountDiv.textContent = 0 | ||
|
||
const originalAddEventListener = window.addEventListener | ||
const originalRemoveEventListener = window.removeEventListener | ||
window.addEventListener = function (name, handler) { | ||
if (name === 'popstate') { | ||
listenerCountDiv.textContent = | ||
++numPopstateListeners | ||
} | ||
return originalAddEventListener.apply(this, arguments) | ||
} | ||
window.removeEventListener = function (name, handler) { | ||
if (name === 'popstate') { | ||
listenerCountDiv.textContent = | ||
--numPopstateListeners | ||
} | ||
return originalRemoveEventListener.apply(this, arguments) | ||
} | ||
|
||
// 1. Use plugin. | ||
// This installs <router-view> and <router-link>, | ||
// and injects $router and $route to all router-enabled child components | ||
Vue.use(VueRouter) | ||
|
||
const looper = [1, 2, 3] | ||
|
||
looper.forEach((n) => { | ||
let vueInstance | ||
const mountEl = document.getElementById('mount' + n) | ||
const unmountEl = document.getElementById('unmount' + n) | ||
|
||
mountEl.addEventListener('click', () => { | ||
// 2. Define route components | ||
const Home = { template: '<div>home</div>' } | ||
const Foo = { template: '<div>foo</div>' } | ||
|
||
// 3. Create the router | ||
const router = new VueRouter({ | ||
mode: 'history', | ||
base: __dirname, | ||
routes: [ | ||
{ path: '/', component: Home }, | ||
{ path: '/foo', component: Foo } | ||
] | ||
}) | ||
|
||
// 4. Create and mount root instance. | ||
// Make sure to inject the router. | ||
// Route components will be rendered inside <router-view>. | ||
vueInstance = new Vue({ | ||
router, | ||
template: ` | ||
<div id="app-${n}"> | ||
<h1>Basic</h1> | ||
<ul> | ||
<li><router-link to="/">/</router-link></li> | ||
<li><router-link to="/foo">/foo</router-link></li> | ||
</ul> | ||
<router-view class="view"></router-view> | ||
</div> | ||
` | ||
}).$mount('#app-' + n) | ||
}) | ||
|
||
unmountEl.addEventListener('click', () => { | ||
vueInstance.$destroy() | ||
vueInstance.$el.innerHTML = '' | ||
}) | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<!DOCTYPE html> | ||
<link rel="stylesheet" href="/global.css"> | ||
<a href="/">← Examples index</a> | ||
|
||
<button id="mount1">Mount App 1</button> | ||
<button id="mount2">Mount App 2</button> | ||
<button id="mount3">Mount App 3</button> | ||
|
||
<hr /> | ||
|
||
<button id="unmount1">Unmount App 1</button> | ||
<button id="unmount2">Unmount App 2</button> | ||
<button id="unmount3">Unmount App 3</button> | ||
|
||
<hr /> | ||
|
||
popstate count: <span id="popcount"></span> | ||
|
||
<div id="app-1"></div> | ||
<div id="app-2"></div> | ||
<div id="app-3"></div> | ||
|
||
<script src="/__build__/shared.chunk.js"></script> | ||
<script src="/__build__/multi-app.js"></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
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
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
Oops, something went wrong.