Skip to content

Commit f295ce1

Browse files
committed
loading routes
1 parent 0bc6df5 commit f295ce1

File tree

6 files changed

+64
-9
lines changed

6 files changed

+64
-9
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
- [Slots](#slots)
2424
- [Scoped Styles](#scoped-styles)
2525
- [Extract Styles](#extract-styles)
26+
- [Routing](#routing)
27+
- [Loading Routes](#loading-routes)
2628

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

@@ -450,3 +452,31 @@ Finally, modify [index.html](index.html) to include the extracted css file:
450452
```html
451453
<link rel="stylesheet" href="/assets/styles.css">
452454
```
455+
456+
## Routing
457+
458+
### Loading Routes
459+
460+
Use [vue-router](https://router.vuejs.org/en/), the official Vue.js plugin for routing. First install it:
461+
462+
```shell
463+
npm install [email protected] --save
464+
```
465+
466+
Vue plugins are scripts that can be used to extend functionality of Vue globally.
467+
468+
[router.js](src/router.js)
469+
470+
Recall in app.js, Layout component is mapped to the Vue app. This means every time a page is loaded, the Layout.vue component is loaded.
471+
472+
Router is exported, then imported into app.js. To use it, extend the Vue instance with the Router instance.
473+
474+
After router is configured to have default path point to Category component, this component can be removed from Layout component and replaced with `<router-view></router-view>` reserved element:
475+
476+
```html
477+
<div class="container content">
478+
<router-view></router-view>
479+
</div>
480+
```
481+
482+
Every time page is loaded, router will determine current path, load component linked to path and add its template in the `<router-view>` section.

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"dependencies": {
1313
"bulma": "0.5.1",
1414
"express": "4.15.4",
15-
"vue": "2.4.2"
15+
"vue": "2.4.2",
16+
"vue-router": "^2.7.0"
1617
},
1718
"devDependencies": {
1819
"babel-core": "6.26.0",

src/app.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import Vue from 'vue'
22
import AppLayout from './theme/Layout.vue'
3+
import router from './router'
34

45
console.log(AppLayout)
56

6-
// `render` property is a Vue.js function that returns an element
7-
// include AppLayout as the rendered result
7+
// extemd Vue instance with router plugin instance
88
const app = new Vue({
9-
render: h => h(AppLayout)
9+
router,
10+
...AppLayout
1011
})
1112

12-
export { app }
13+
// export router as well because it may be needed by client entry later
14+
export { app, router }

src/router.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import Vue from 'vue'
2+
import VueRouter from 'vue-router'
3+
import Category from './theme/Category.vue'
4+
5+
// connect VueRouter plugin with Vue configuration
6+
Vue.use(VueRouter)
7+
8+
// configure router with an object of options
9+
// `router` is a property of options object to specify an array
10+
// this array will link routes with components
11+
// any component referenced in routes must be imported above
12+
// recall in app.js, Layout component is mapped to the Vue app
13+
const router = new VueRouter({
14+
routes: [
15+
{path: '/', component: Category}
16+
]
17+
})
18+
19+
export default router

src/theme/Layout.vue

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<app-header></app-header>
44
<section class="main-section section">
55
<div class="container content">
6-
<category></category>
6+
<router-view></router-view>
77
</div>
88
</section>
99
<app-footer></app-footer>
@@ -13,12 +13,10 @@
1313
<script>
1414
import AppHeader from './AppHeader.vue'
1515
import AppFooter from './AppFooter.vue'
16-
import Category from './Category.vue'
1716
export default {
1817
components: {
1918
'app-header': AppHeader,
20-
'app-footer': AppFooter,
21-
'category': Category
19+
'app-footer': AppFooter
2220
}
2321
}
2422
</script>

0 commit comments

Comments
 (0)