Skip to content

Commit 6632fcf

Browse files
committed
wildcard route
1 parent 55803ea commit 6632fcf

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
- [Redirect](#redirect)
3131
- [Route Parameters](#route-parameters)
3232
- [Route Query and Name](#route-query-and-name)
33+
- [Wildcard Route](#wildcard-route)
3334

3435
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
3536

@@ -634,3 +635,11 @@ However, now clicking on Mobile link in app, this shows up in url: `http://local
634635
The json got encoded and placed in url. To fix this, put `:` in front of `to` attribute of `<router-link>`: `<router-link class="nav-item is-tab" :to="{name: 'category', params: {id: 'mobile'}}">Mobile</router-link>`
635636

636637
Now Vue can parse the section rather than adding it as a string to url.
638+
639+
### Wildcard Route
640+
641+
For example, to create 404 page for all paths that aren't explicitly handled, use wildcard route, linking it to any component you wish, in this example, [NotFound](src/theme/NotFound.vue) which simply displays an error message:
642+
643+
```javascript
644+
{path: '*', component: NotFound}
645+
```

src/router.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Vue from 'vue'
22
import VueRouter from 'vue-router'
33
import Category from './theme/Category.vue'
44
import Login from './theme/Login.vue'
5+
import NotFound from './theme/NotFound.vue'
56

67
// connect VueRouter plugin with Vue configuration
78
Vue.use(VueRouter)
@@ -21,7 +22,8 @@ const router = new VueRouter({
2122
routes: [
2223
{path: '/login', component: Login},
2324
{path: '/category/:id', name: 'category', component: Category},
24-
{path: '/', redirect: '/category/front-end'}
25+
{path: '/', redirect: '/category/front-end'},
26+
{path: '*', component: NotFound}
2527
]
2628
})
2729

src/theme/NotFound.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template>
2+
<div class="error">Oops, page not found!</div>
3+
</template>

0 commit comments

Comments
 (0)