在TypeScript和Vue SFC中使用全局枚举

时间:2020-11-10 15:26:26

标签: typescript vue.js

我试图用TypeScript声明一个枚举,然后在Vue SFC中使用它。使用枚举时它可以工作,例如ModalType.None中的<template>,但<script>中没有。我收到此错误

未在src / components / Modal.vue:17:30:

定义'ModalType'(no-undef)

为什么它在模板中有效,但在脚本中无效?我不想每次使用时都导入类型,这就是为什么我使用global.d.ts

我看着How to use Typescript global types declarations in Vue SFC?,但是该解决方案无效。

global.d.ts

declare const enum ModalType {
  None,
  Success,
  Danger,
  Warning,
}

Modal.vue

<template>
  <div v-if="type === ModalType.None"></div>
</template>

<script lang="ts">
  import { defineComponent, PropType } from 'vue';

  export default defineComponent({
    props: {
      type: {
        type: String as PropType<ModalType>,
        default: null,
      },
    },
    computed: {
      isNone(): boolean {
        return this.type !== ModalType.None;
      },
    },
  });
</script>

0 个答案:

没有答案