【发布时间】:2019-06-15 04:59:48
【问题描述】:
我想为每条路由动态设置窗口的标题,所以在每个routes: [] 子对象中我都有一个meta: { title: ... } 对象。例如:
routes: [
{
path: 'profile/:id',
name: 'Profile',
component: Profile,
meta: {
title: function (to, cb) {
const profileId = parseInt(to.params.id);
// ... do stuff ...
}
}
}
]
我在 afterEach 钩子中调用这个标题函数:
router.afterEach((to) => {
document.title = 'My Site';
if (to.meta && to.meta.title) {
to.meta.title(router.app, to, (result) => { document.title += ' | ' + result; });
}
});
在... do stuff ... 部分,我想从我的mixin GetAndStore.js 调用一个名为loadProfile(profileId) 的方法。我将GetAndStore 添加到路由器的mixin 中,但loadProfile 不可用(this.loadProfile 未定义)。我在全局范围内加载了GetAndStore 并再次尝试了相同的结果。在过去的一个小时里,我已经尝试了所有我能想到的配置,但我根本没有找到任何方法可以在此设置中访问来自 GetAndStore 的方法。
关于我缺少什么或者我需要重组什么以便从 routes->element->meta->title 中访问 mixin 方法的任何想法?
【问题讨论】:
-
你能展示你的mixin代码吗?你的
loadProfile方法有什么依赖关系? -
@Phil
loadProfile是超级基本的。它发出axiosGET 请求,然后用数据解析。它只依赖于axios。
标签: javascript vue.js vue-router mixins vue-mixin