如何解决如何使用TypeScript将钩子添加到Nuxt路由?
我正在尝试了解Nuxt路由,以便在加载页面之前可以做一些魔术。但是,我似乎找不到有关如何执行此操作的TypeScript示例。下面的示例不起作用:
<template>
<div class="some-css">
{{ text }}
</div>
</template>
<script lang="ts">
import Vue from "vue"
export default class MyComponent extends Vue {
text: string = "My Component"
beforeRouteEnter(to,from,next): void {
console.log(`Routing from: ${ from },to: ${ to }`)
next()
}
}
</script>
<style>
.some-css {
color: #000000;
}
</style>
是否可以使用TypeScript在此组件中或在Nuxt中的全局级别上做到这一点?
解决方法
这是我通常使用nuxt-property-decorator进行的操作:
<script>
import Vue from "vue"
import { Component } from 'nuxt-property-decorator'
@Component({
beforeRouteUpdate(to,from) {
// do stuff
}
})
export default class MyComponent extends Vue {
}
</script>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。