admin管理员组文章数量:1201202
I have integrated intercom in my app and I need to call window.Intercom('update');
every-time my url changes.
I know I could add it on mounted()
but I rather not modify all my component and do it directly using the navigation guards. (Mainly to avoid to have the same code in 10 different places.
At the moment I have:
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
})
This runs intercom('update')
before changing the url, while I need to run it after the url changes.
Is there a hook which runs just when the url has changed? How can I do this?
Thanks
I have integrated intercom in my app and I need to call window.Intercom('update');
every-time my url changes.
I know I could add it on mounted()
but I rather not modify all my component and do it directly using the navigation guards. (Mainly to avoid to have the same code in 10 different places.
At the moment I have:
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
})
This runs intercom('update')
before changing the url, while I need to run it after the url changes.
Is there a hook which runs just when the url has changed? How can I do this?
Thanks
Share Improve this question asked Aug 10, 2017 at 0:26 CostantinCostantin 2,6468 gold badges34 silver badges52 bronze badges 13 | Show 8 more comments3 Answers
Reset to default 21Wasn't sure this would work as what you already have seems like it should be fine but here goes...
Try watching the $route
object for changes
new Vue({
// ...
watch: {
'$route': function(to, from) {
Intercom('update')
}
}
})
I just came up with another solution beyond Phil's, you could also use Global Mixin. It merges its methods or lifecycle hooks into every component.
Vue.mixin({
mounted() {
// do what you need
}
})
created() {
this.$watch(
() => this.$route.params,
() => /*Your Function*/
);
},
本文标签: javascriptcall a function every time a route is updated vuejsStack Overflow
版权声明:本文标题:javascript - call a function every time a route is updated vue.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738597202a2101854.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$route
object in yourVue
instance. – Phil Commented Aug 10, 2017 at 0:37console.log(window.location.href )
print the previous url. e.g. from home I click on /about, it prints home, then I click on /team it prints /about etc – Costantin Commented Aug 10, 2017 at 0:39watch
worked perfectly. Thanks. – Costantin Commented Aug 10, 2017 at 0:53