【问题标题】:How to add a hook to Nuxt routing using TypeScript?如何使用 TypeScript 为 Nuxt 路由添加钩子?
【发布时间】:2020-12-21 12:53:10
【问题描述】:

我试图在 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>

有没有办法在这个组件中或在 Nuxt 的全局级别上使用 TypeScript 做到这一点?

【问题讨论】:

    标签: typescript vue.js nuxt.js


    【解决方案1】:

    这是我通常使用 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>
    

    【讨论】:

    • 啊是的,我忘了提到我确实使用了@Component,虽然它是 vue-property-decorator 导入
    • 我试过了,可惜没用...
    猜你喜欢
    • 2019-03-18
    • 1970-01-01
    • 2020-05-15
    • 1970-01-01
    • 2021-07-06
    • 1970-01-01
    • 2022-10-08
    • 1970-01-01
    • 2020-08-29
    相关资源
    最近更新 更多