[Vuejs]-Vue.js pass data to route using beforeRouteUpdate

0👍

As far as I know, the function beforeRouteUpdate is a component hook. Then, you can’t use this inside your router. But you can pass a param to your route, via query.

Your router:

routes: [
      {
          path: '/dashboard',
          name: 'dashboard',
          query: {title: 'dashboard'},
          component: function () {  return import( './views/dashboard.vue')  }, 
      },
        {
          path: '/home',
          name: 'home',
          query: {title: 'home'},
          component: function () {  return import( './views/home.vue')  },
        },
]

Your component:

data(){
  return { 
    documentTitle:  '', 
  }
},
mounted() {
    this.documentTitle = this.$route.query.title;
},

In other way, you can use your path name inside your component. You even don’t need to declare query in your router.

data(){
  return { 
    documentTitle:  '', 
  }
},
mounted() {
    this.documentTitle = this.$route.path.name;
},

Leave a comment