Skip to content

Commit ea7bffd

Browse files
authored
Migrate: Routing page (#79)
1 parent 1bd5ef8 commit ea7bffd

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

src/.vuepress/config.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,15 @@ const sidebar = {
4141
{
4242
title: 'Reusability & Composition',
4343
collapsable: false,
44-
children: ['/guide/mixins', '/guide/custom-directive']
44+
children: [
45+
'/guide/mixins',
46+
'/guide/custom-directive'
47+
]
48+
},
49+
{
50+
title: 'Scaling Up',
51+
collapsable: false,
52+
children: ['/guide/routing']
4553
},
4654
{
4755
title: 'Migration to Vue 3',

src/guide/routing.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Routing
2+
3+
## Official Router
4+
5+
For most Single Page Applications, it's recommended to use the officially-supported [vue-router library](https://github.com/vuejs/vue-router). For more details, see vue-router's [documentation](https://router.vuejs.org/).
6+
7+
## Simple Routing from Scratch
8+
9+
If you only need very simple routing and do not wish to involve a full-featured router library, you can do so by dynamically rendering a page-level component like this:
10+
11+
``` js
12+
const NotFoundComponent = { template: '<p>Page not found</p>' }
13+
const HomeComponent = { template: '<p>Home page</p>' }
14+
const AboutComponent = { template: '<p>About page</p>' }
15+
16+
const routes = {
17+
'/': HomeComponent,
18+
'/about': AboutComponent
19+
}
20+
21+
const SimpleRouterApp = {
22+
data: () => ({
23+
currentRoute: window.location.pathname
24+
}),
25+
26+
computed: {
27+
CurrentComponent () {
28+
return routes[this.currentRoute] || NotFoundComponent
29+
}
30+
},
31+
32+
render () {
33+
return Vue.h(this.CurrentComponent)
34+
}
35+
}
36+
37+
Vue.createApp(SimpleRouterApp).mount('#app')
38+
```
39+
40+
Combined with the [History API](https://developer.mozilla.org/en-US/docs/Web/API/History_API/Working_with_the_History_API), you can build a very basic but fully-functional client-side router. To see that in practice, check out [this example app](https://github.com/phanan/vue-3.0-simple-routing-example).
41+
42+
## Integrating 3rd-Party Routers
43+
44+
If there's a 3rd-party router you prefer to use, such as [Page.js](https://github.com/visionmedia/page.js) or [Director](https://github.com/flatiron/director), integration is [similarly straightforward](https://github.com/phanan/vue-3.0-simple-routing-example/compare/master...pagejs). Here's a [complete example](https://github.com/phanan/vue-3.0-simple-routing-example/tree/pagejs) using Page.js.

0 commit comments

Comments
 (0)