[英]Use enum for default value of defineProps
This code was working just fine with vue3 and typescript:这段代码在 vue3 和 typescript 上运行得很好:
<script lang="ts">
import Vue, { PropType } from 'vue'
enum Position {
TOP = 'top',
RIGHT = 'right',
BOTTOM = 'bottom',
LEFT = 'left',
}
export default {
props: {
position: {
type: String as PropType<Position>,
default: Position.BOTTOM,
},
},
};
</script>
But equivalent code in <script setup>
syntax:但<script setup>
语法中的等效代码:
<script setup lang="ts">
enum Activator {
PERSIST = 'persist',
DEMAND = 'demand',
HOVER = 'hover'
}
type Props = {
activator?: Activator
}
const props = withDefaults(defineProps<Props>(), {
activator: Activator.HOVER
});
</script>
The script has less boilerplate but also doesn't work with an error:该脚本的样板文件较少,但也不会出现错误:
Error: [@vue/compiler-sfc] `defineProps()` in cannot reference locally declared variables because it will be hoisted outside of the setup() function. 错误:[@vue/compiler-sfc] `defineProps()` in 不能引用本地声明的变量,因为它将被提升到 setup() function 之外。 If your component options require initialization in the module scope, use a separate normal to export the options instead. 如果您的组件选项需要在模块 scope 中初始化,请改用单独的法线导出选项。 ``` App.vue 13 | ``` App.vue 13 | 14 | 14 | const props = withDefaults(defineProps(), { 15 | activator: Activator.HOVER | ^^^^^^^^^ 16 | }); const props = withDefaults(defineProps(), { 15 | 激活器:Activator.HOVER | ^^^^^^^^^ 16 | }); 17 | 17 | at Se (https://sfc.vuejs.org/assets/index.8aa136ef.js:137:75) at https://sfc.vuejs.org/assets/index.8aa136ef.js:141:1419 at Object.enter (https://sfc.vuejs.org/assets/index.8aa136ef.js:76:6842) ``` 在 Se (https://sfc.vuejs.org/assets/index.8aa136ef.js:137:75) 在 https://sfc.vuejs.org/assets/index.8aa136ef.js:141:1419 在 Z497031794414A552435F90153B54输入(https://sfc.vuejs.org/assets/index.8aa136ef.js:76:6842)```
Isn't it possible to use enums for default value with script setup?脚本设置不能使用枚举作为默认值吗? Playground 操场
EDIT: It seems that after I export the type from other files it doesn't crash.编辑:似乎在我从其他文件中导出类型后它不会崩溃。 I'll investigate a little further.我会进一步调查。
It seems that this synax doesn't support the enum
s according to this根据这个,这个语法似乎不支持enum
s
As of now, the type declaration argument must be one of the following to ensure correct static analysis:截至目前,类型声明参数必须是以下之一,以确保正确的 static 分析:
- A type literal类型文字
- A reference to an interface or a type literal in the same file对同一文件中的接口或类型文字的引用
You could use a basic type:您可以使用基本类型:
<script setup lang="ts">
type Activator = "persist" | "demand" | "hover";
type Props = {
activator?: Activator;
};
withDefaults(defineProps<Props>(), {
activator: "demand",
});
</script>
<template>
<div>{{ activator }}</div>
</template>
Define the enum
in an other (non-setup) script.在其他(非设置)脚本中定义enum
。
<script setup lang="ts">
type Props = {
activator?: Activator
}
const props = withDefaults(defineProps<Props>(), {
activator: Activator.HOVER·
});
</script>
<script lang="ts"> // here
enum Activator {
PERSIST = 'persist',
DEMAND = 'demand',
HOVER = 'hover'
}
</script>
<template>
<h1>hello world</h1>
</template>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.