Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(router-view): render children as placeholder #2799

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 45 additions & 0 deletions examples/placeholder/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const Home = { template: '<div id="home">This is Home</div>' }
const Header = { template: '<div id="header">This is Header</div>' }
const Footer = { template: '<div id="footer">This is Footer</div>' }

const router = new VueRouter({
mode: 'history',
base: __dirname,
routes: [
{
path: '/',
components: {
header: Header,
default: Home,
footer: Footer
}
}
]
})

router.beforeEach((to, from, next) => {
setTimeout(() => {
next()
}, 1000)
})

new Vue({
router,
template: `
<div id="app">
<h1>router-view placeholder</h1>
<router-view name="header">
<div id="header-loading" class="placeholder">Loading header...</div>
</router-view>
<router-view>
<div id="default-loading" class="placeholder">Loading default...</div>
</router-view>
<router-view name="footer"></router-view>
</div>
`
}).$mount('#app')
6 changes: 6 additions & 0 deletions examples/placeholder/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!DOCTYPE html>
<link rel="stylesheet" href="/global.css">
<a href="/">&larr; Examples index</a>
<div id="app"></div>
<script src="/__build__/shared.chunk.js"></script>
<script src="/__build__/placeholder.js"></script>
9 changes: 9 additions & 0 deletions src/components/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ export default {
// render empty node if no matched route
if (!matched) {
cache[name] = null

// The component child may act as the temporary placeholder
// up until the actual component from the route takes place.
// Using the first child because the actual component can only
// have one single element as the root anyway.
if (children && children.length === 1) {
return children[0]
}

return h()
}

Expand Down
22 changes: 22 additions & 0 deletions test/e2e/specs/placeholder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = {
'placeholder': function (browser) {
browser
.url('http://localhost:8080/placeholder/')
.waitForElementVisible('#app', 1000)

// Assert that 2 elements exist
// because the last one doesn't have a placeholder
.assert.count('#app div.placeholder', 2)
.assert.containsText('#header-loading', 'Loading header...')
.assert.containsText('#default-loading', 'Loading default...')

// When the timeout ends, assert if the components are there
.waitForElementVisible('#home', 1000)
.assert.count('#app div.placeholder', 0)
.assert.containsText('#header', 'This is Header')
.assert.containsText('#home', 'This is Home')
.assert.containsText('#footer', 'This is Footer')

.end()
}
}