1- import { createRouter , createWebHistory } from 'vue-router'
2- import Home from '../components/Home.vue'
1+ import { createRouter , createWebHistory } from 'vue-router' ;
2+ import Home from '../components/Home.vue' ;
33
44const routes = [
5- {
6- path : '/' ,
7- name : 'Home' ,
8- component : Home ,
9- meta : {
10- title : 'Home'
11- }
12- } ,
13- {
14- path : '/about' ,
15- name : 'About' ,
16- // route level code-splitting
17- // this generates a separate chunk (about.[hash].js) for this route
18- // which is lazy-loaded when the route is visited.
19- component : ( ) => import ( /* webpackChunkName: "about" */ '../components/About.vue' ) ,
20- meta : {
21- title : 'About'
22- }
23- } ,
24- {
25- path : '/projects' ,
26- name : 'Projects' ,
27- // route level code-splitting
28- // this generates a separate chunk (projects.[hash].js) for this route
29- // which is lazy-loaded when the route is visited.
30- component : ( ) => import ( /* webpackChunkName: "projects" */ '../components/Projects.vue' ) ,
31- meta : {
32- title : 'Projects'
33- }
34- }
35- ]
5+ {
6+ path : '/' ,
7+ name : 'Home' ,
8+ component : Home ,
9+ meta : {
10+ title : 'Home' ,
11+ } ,
12+ } ,
13+ {
14+ path : '/about' ,
15+ name : 'About' ,
16+ // route level code-splitting
17+ // this generates a separate chunk (about.[hash].js) for this route
18+ // which is lazy-loaded when the route is visited.
19+ component : ( ) =>
20+ import ( /* webpackChunkName: "about" */ '../components/About.vue' ) ,
21+ meta : {
22+ title : 'About' ,
23+ } ,
24+ } ,
25+ {
26+ path : '/projects' ,
27+ name : 'Projects' ,
28+ // route level code-splitting
29+ // this generates a separate chunk (projects.[hash].js) for this route
30+ // which is lazy-loaded when the route is visited.
31+ component : ( ) =>
32+ import (
33+ /* webpackChunkName: "projects" */ '../components/Projects.vue'
34+ ) ,
35+ meta : {
36+ title : 'Projects' ,
37+ } ,
38+ } ,
39+ {
40+ path : '/projects/single-project' ,
41+ name : 'Single Project' ,
42+ // route level code-splitting
43+ // this generates a separate chunk (projects.[hash].js) for this route
44+ // which is lazy-loaded when the route is visited.
45+ component : ( ) =>
46+ import (
47+ /* webpackChunkName: "projects" */ '../components/SingleProject.vue'
48+ ) ,
49+ meta : {
50+ title : 'Single Project' ,
51+ } ,
52+ } ,
53+ ] ;
3654
3755const router = createRouter ( {
38- history : createWebHistory ( process . env . BASE_URL ) ,
39- routes
40- } )
56+ history : createWebHistory ( process . env . BASE_URL ) ,
57+ routes,
58+ } ) ;
4159
42- export default router
60+ export default router ;
4361
4462/**
4563 * Below code will display the component/active page title
@@ -48,44 +66,56 @@ export default router
4866
4967// This callback runs before every route change, including on page load.
5068router . beforeEach ( ( to , from , next ) => {
51- // This goes through the matched routes from last to first, finding the closest route with a title.
52- // e.g., if we have `/some/deep/nested/route` and `/some`, `/deep`, and `/nested` have titles,
53- // `/nested`'s will be chosen.
54- const nearestWithTitle = to . matched . slice ( ) . reverse ( ) . find ( r => r . meta && r . meta . title ) ;
69+ // This goes through the matched routes from last to first, finding the closest route with a title.
70+ // e.g., if we have `/some/deep/nested/route` and `/some`, `/deep`, and `/nested` have titles,
71+ // `/nested`'s will be chosen.
72+ const nearestWithTitle = to . matched
73+ . slice ( )
74+ . reverse ( )
75+ . find ( ( r ) => r . meta && r . meta . title ) ;
5576
56- // Find the nearest route element with meta tags.
57- const nearestWithMeta = to . matched . slice ( ) . reverse ( ) . find ( r => r . meta && r . meta . metaTags ) ;
77+ // Find the nearest route element with meta tags.
78+ const nearestWithMeta = to . matched
79+ . slice ( )
80+ . reverse ( )
81+ . find ( ( r ) => r . meta && r . meta . metaTags ) ;
5882
59- const previousNearestWithMeta = from . matched . slice ( ) . reverse ( ) . find ( r => r . meta && r . meta . metaTags ) ;
83+ const previousNearestWithMeta = from . matched
84+ . slice ( )
85+ . reverse ( )
86+ . find ( ( r ) => r . meta && r . meta . metaTags ) ;
6087
61- // If a route with a title was found, set the document (page) title to that value.
62- if ( nearestWithTitle ) {
63- document . title = nearestWithTitle . meta . title ;
64- } else if ( previousNearestWithMeta ) {
65- document . title = previousNearestWithMeta . meta . title ;
66- }
88+ // If a route with a title was found, set the document (page) title to that value.
89+ if ( nearestWithTitle ) {
90+ document . title = nearestWithTitle . meta . title ;
91+ } else if ( previousNearestWithMeta ) {
92+ document . title = previousNearestWithMeta . meta . title ;
93+ }
6794
68- // Remove any stale meta tags from the document using the key attribute we set below.
69- Array . from ( document . querySelectorAll ( '[data-vue-router-controlled]' ) ) . map ( el => el . parentNode . removeChild ( el ) ) ;
95+ // Remove any stale meta tags from the document using the key attribute we set below.
96+ Array . from (
97+ document . querySelectorAll ( '[data-vue-router-controlled]' )
98+ ) . map ( ( el ) => el . parentNode . removeChild ( el ) ) ;
7099
71- // Skip rendering meta tags if there are none.
72- if ( ! nearestWithMeta ) return next ( ) ;
100+ // Skip rendering meta tags if there are none.
101+ if ( ! nearestWithMeta ) return next ( ) ;
73102
74- // Turn the meta tag definitions into actual elements in the head.
75- nearestWithMeta . meta . metaTags . map ( tagDef => {
76- const tag = document . createElement ( 'meta' ) ;
103+ // Turn the meta tag definitions into actual elements in the head.
104+ nearestWithMeta . meta . metaTags
105+ . map ( ( tagDef ) => {
106+ const tag = document . createElement ( 'meta' ) ;
77107
78- Object . keys ( tagDef ) . forEach ( key => {
79- tag . setAttribute ( key , tagDef [ key ] ) ;
80- } ) ;
108+ Object . keys ( tagDef ) . forEach ( ( key ) => {
109+ tag . setAttribute ( key , tagDef [ key ] ) ;
110+ } ) ;
81111
82- // We use this to track which meta tags we create so we don't interfere with other ones.
83- tag . setAttribute ( 'data-vue-router-controlled' , '' ) ;
112+ // We use this to track which meta tags we create so we don't interfere with other ones.
113+ tag . setAttribute ( 'data-vue-router-controlled' , '' ) ;
84114
85- return tag ;
86- } )
87- // Add the meta tags to the document head.
88- . forEach ( tag => document . head . appendChild ( tag ) ) ;
115+ return tag ;
116+ } )
117+ // Add the meta tags to the document head.
118+ . forEach ( ( tag ) => document . head . appendChild ( tag ) ) ;
89119
90- next ( ) ;
120+ next ( ) ;
91121} ) ;
0 commit comments