Navigating Vue Router

A presentation at Chicago VueJS Meetup in January 2019 in Chicago, IL, USA by Divya

Slide 1

Slide 1

Navigating Vue Router

Slide 2

Slide 2

Divya Sasidharan Developer Advocate @shortdiv

Slide 3

Slide 3

Why

Slide 4

Slide 4

Routing is essentially the way you navigate through a website or web-application.

Slide 5

Slide 5

Client Side Routing

Slide 6

Slide 6

Client Side Routing vs Server Side Routing

Slide 7

Slide 7

Server Side Routing Client Side Routing ! ! ! Fast time to first paint Fast Routing Optimized for SEO Smooth Transitions " Full page refresh " Slower routing " ! longer time to first paint " Not SEO friendly

Slide 8

Slide 8

SPA = Client Side Routing

Slide 9

Slide 9

What

Slide 10

Slide 10

Vue Router 🚦

Slide 11

Slide 11

. β”œβ”€β”€ src/ β”œβ”€β”€ … β”œβ”€β”€ router.js β”œβ”€β”€ … β”œβ”€β”€ … └── package.json

Slide 12

Slide 12

. β”œβ”€β”€ src/ β”œβ”€β”€ … β”œβ”€β”€ router/ β”œβ”€β”€ index.js β”œβ”€β”€ routes.js β”œβ”€β”€ … β”œβ”€β”€ … └── package.json

Slide 13

Slide 13

How

Slide 14

Slide 14

export default [ { path: "/", name: "dashboard", component: Dashboard }, ] src/router/routes.js

Slide 15

Slide 15

import Vue from "vue"; import Router from "vue-router"; import routes from "./routes"; Vue.use(Router); const router = new Router({ routes, mode: "history" }); export default router; src/router/index.js

Slide 16

Slide 16

import Vue from "vue"; import App from "./App.vue"; import router from "./router"; new Vue({ router, render: h %=> h(App) }).$mount("#app"); src/main.js

Slide 17

Slide 17

When

Slide 18

Slide 18

β€’ Nested route/view mapping β€’ Modular, component-based router configuration β€’ Route params, query, wildcards β€’ View transition effects powered by Vue.js' transition system β€’ Fine-grained navigation control β€’ Links with automatic active CSS classes β€’ HTML5 history mode or hash mode, with auto-fallback in IE9 β€’ Customizable Scroll Behavior

Slide 19

Slide 19

β€’ Nested route/view mapping β€’ Modular, component-based router configuration β€’ Route params, query, wildcards β€’ View transition effects powered by Vue.js' transition system β€’ Fine-grained navigation control β€’ Links with automatic active CSS classes β€’ HTML5 history mode or hash mode, with auto-fallback in IE9 β€’ Customizable Scroll Behavior

Slide 20

Slide 20

Navigation Guards

Slide 21

Slide 21

Demo

Slide 22

Slide 22

Slide 23

Slide 23

Navigation Guards Global Per Route In Component

Slide 24

Slide 24

Global Hooks … const router = new Router({ routes, mode: "history" }); router.beforeEach((to, from, next) %=> { %// … } router.beforeResolve((to, from, next) %=> { %// … } router.afterEach((to, from, next) %=> { %// … } src/router/index.js

Slide 25

Slide 25

Per Guard Hooks export default [ { path: β€œ/", … beforeEnter(to, from, next) { // … }, beforeResolve(to, from, next) { // … } afterEach(to, from, next) { // … } }, src/router/routes.js ]

Slide 26

Slide 26

In Component Hooks export default { name: β€œDashboard”, … beforeRouteEnter(to, from, next) { // … }, beforeRouteUpdate(to, from, next) { // … }, beforeRouteLeave(to, from, next) { // … } } src/router/routes.js

Slide 27

Slide 27

Thanks! @shortdiv https://github.com/shortdiv/gotruejs-in-vue