Currently, I have a Single Page Application (SPA) built with Vue and Vue Router. The setup mimics an iOS app where the title in the navigation bar changes as you navigate deeper into the views. Right now, the titles are hardcoded in the routes.js file, but I'd like to make them dynamic. I tried updating the route meta within my view, but it renders after the view is shown, so I need it to happen before the route change occurs. Here's how it's currently set up:
routes.js
// Example of a route
{
path: '/team/:username',
component: require('./views/team-single'),
name: 'profile',
meta:{ requiresAuth: true, title: 'Team Member', tabbar: false }
}
navbar.vue
// If the route has a title, show it; otherwise show the logo
<div class="navbar-item">
<transition name="fadeup">
<h1 v-if="$route.meta.title" class="navbar-title">{{ $route.meta.title }}</h1>
<img src="/img/hopbak-green.svg" class="navbar-logo" alt="hopbak" v-else>
</transition>
</div>
Ideally, it would be great to pass the :username parameter into the title in the route.js, but that doesn't seem possible. I attempted to add something like this in the view, but as mentioned earlier, it gets called too late:
team-member.vue
mounted(){
this.$route.meta.title = this.user.username
}
This code does change the title, however, it happens after the navbar has already loaded.