使用 Typescript 观察 Vue.js 中的路由变化 [英] Watch route changes in Vue.js with Typescript

查看:41
本文介绍了使用 Typescript 观察 Vue.js 中的路由变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 TypeScript 检测 Vue.js 应用程序中 URL 的变化.在 JavaScript 中,这可以按如下方式完成:

I'd like to detect changes in my URL in a Vue.js application using TypeScript. In JavaScript this can be done as follows:

watch: {
  '$route': {
     handler: 'onUrlChange',
     immediate: true,
     deep: true
   }
},

onUrlChange(newUrl){
   // Some action
}

我如何在 TypeScript 中做同样的事情?

How could I do the same in TypeScript?

推荐答案

我在这里找到了解决方案:https://github.com/kaorun343/vue-property-decorator

I found the solution here: https://github.com/kaorun343/vue-property-decorator

可以这样做:

   @Watch('$route', { immediate: true, deep: true })
   onUrlChange(newVal: any) {
      // Some action
    }

这篇关于使用 Typescript 观察 Vue.js 中的路由变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆