A presentation at Chicago VueJS Meetup in January 2019 in Chicago, IL, USA by Divya
Navigating Vue Router
Divya Sasidharan Developer Advocate @shortdiv
Why
Routing is essentially the way you navigate through a website or web-application.
Client Side Routing
Client Side Routing vs Server Side Routing
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
SPA = Client Side Routing
What
Vue Router π¦
. βββ src/ βββ β¦ βββ router.js βββ β¦ βββ β¦ βββ package.json
. βββ src/ βββ β¦ βββ router/ βββ index.js βββ routes.js βββ β¦ βββ β¦ βββ package.json
How
export default [ { path: "/", name: "dashboard", component: Dashboard }, ] src/router/routes.js
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
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
When
β’ 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
Navigation Guards
Demo
Navigation Guards Global Per Route In Component
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
Per Guard Hooks export default [ { path: β/", β¦ beforeEnter(to, from, next) { // β¦ }, beforeResolve(to, from, next) { // β¦ } afterEach(to, from, next) { // β¦ } }, src/router/routes.js ]
In Component Hooks export default { name: βDashboardβ, β¦ beforeRouteEnter(to, from, next) { // β¦ }, beforeRouteUpdate(to, from, next) { // β¦ }, beforeRouteLeave(to, from, next) { // β¦ } } src/router/routes.js
Thanks! @shortdiv https://github.com/shortdiv/gotruejs-in-vue
View Navigating Vue Router on Notist.
Dismiss