【发布时间】:2018-01-18 01:09:40
【问题描述】:
我在我的应用程序中集成了对讲机,每次我的网址更改时我都需要致电window.Intercom('update');。
我知道我可以将它添加到mounted(),但我宁愿不修改我的所有组件,而是直接使用navigation guards 来完成。 (主要是为了避免在 10 个不同的地方有相同的代码。
目前我有:
router.afterEach((to, from) => {
eventHub.$off(); // I use this for an other things, here is not important
console.log(window.location.href ) // this prints the previous url
window.Intercom('update'); // which means that this also uses the previous url
})
这在更改 url 之前运行 intercom('update'),而我需要在 url 更改后运行它。
是否有一个钩子在 url 更改时运行? 我该怎么做?
谢谢
【问题讨论】:
-
您可以在
Vue实例中尝试 watching the$routeobject。 -
@thanksd 我说它还没有改变,因为在第 3 行
console.log(window.location.href )打印了以前的 url。例如从家里我点击 /about,它打印 home,然后我点击 /team 它打印 /about 等 -
@Phil 你的意思是在我的基础组件中添加一个观察者?这可以工作.. 我通常会尽量避免观察者,但在这里它可能是有意义的
-
为什么不给 App.vue 添加一个挂载的钩子。所有其他组件都是 App.vue 的子组件
-
@KamgaSimoJunior @Phil
watch工作得很好。谢谢。
标签: javascript vuejs2 vue-component vue-router intercom