@@ -5,15 +5,32 @@ const routes = [
5
5
{
6
6
path : '/' ,
7
7
name : 'Home' ,
8
- component : Home
8
+ component : Home ,
9
+ meta : {
10
+ title : 'Home'
11
+ }
9
12
} ,
10
13
{
11
14
path : '/about' ,
12
15
name : 'About' ,
13
16
// route level code-splitting
14
17
// this generates a separate chunk (about.[hash].js) for this route
15
18
// which is lazy-loaded when the route is visited.
16
- component : ( ) => import ( /* webpackChunkName: "about" */ '../components/About.vue' )
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
+ }
17
34
}
18
35
]
19
36
@@ -23,3 +40,52 @@ const router = createRouter({
23
40
} )
24
41
25
42
export default router
43
+
44
+ /**
45
+ * Below code will display the component/active page title
46
+ * Powered by: Nangialai Stoman
47
+ */
48
+
49
+ // This callback runs before every route change, including on page load.
50
+ router . 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 ) ;
55
+
56
+ // Find the nearest route element with meta tags.
57
+ const nearestWithMeta = to . matched . slice ( ) . reverse ( ) . find ( r => r . meta && r . meta . metaTags ) ;
58
+
59
+ const previousNearestWithMeta = from . matched . slice ( ) . reverse ( ) . find ( r => r . meta && r . meta . metaTags ) ;
60
+
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
+ }
67
+
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 ) ) ;
70
+
71
+ // Skip rendering meta tags if there are none.
72
+ if ( ! nearestWithMeta ) return next ( ) ;
73
+
74
+ // Turn the meta tag definitions into actual elements in the head.
75
+ nearestWithMeta . meta . metaTags . map ( tagDef => {
76
+ const tag = document . createElement ( 'meta' ) ;
77
+
78
+ Object . keys ( tagDef ) . forEach ( key => {
79
+ tag . setAttribute ( key , tagDef [ key ] ) ;
80
+ } ) ;
81
+
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' , '' ) ;
84
+
85
+ return tag ;
86
+ } )
87
+ // Add the meta tags to the document head.
88
+ . forEach ( tag => document . head . appendChild ( tag ) ) ;
89
+
90
+ next ( ) ;
91
+ } ) ;
0 commit comments