【发布时间】:2018-04-01 00:13:19
【问题描述】:
所以我有一个使用 Vue 和 Vue Router 的 SPA。它的设置类似于 iOS 应用程序,当您深入点击视图时,导航栏中的标题会发生变化。现在,我在 routes.js 文件中对此进行了硬编码,但想让它动态化。我可以在我的视图中更新路由元,但是在视图显示之后呈现,所以我需要它在路由更改之前发生。这是设置:
route.js
//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 that not 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>
所以理想情况下,最好将 :username 传递到 route.js 中的标题中,但我认为这是不可能的。我曾尝试将这样的内容添加到视图中,但就像我说的那样,它被调用得很晚:
团队成员.vue
mounted(){
this.$route.meta.title = this.user.username
}
确实会改变它,但不会在导航栏已经加载之前。
【问题讨论】:
-
你能加个
beforeRouteEnter守卫吗?
标签: vue.js vue-router