I am developing with Vue.js 3.0 and TypeScript.
I want to define an enum to be used in multiple components.
How and where should I write in the file?
Directory
src/
├ components/
│ ├ ParentComponent.vue
│ └ ChildComponent.vue
└ App.vue
ChildComponent.vue
<template>
<p>{{ color }}</p>
</template>
<script lang="ts">
import { defineComponent, PropType } from 'vue'
enum Color {
red = 1,
blue
}
export default defineComponent({
props: {
color: { type: Color, required: true }
}
})
</script>
ParentComponent.vue
<template>
<div>
<ChildComponent
:color="state.color"
/>
</div>
</template>
<script lang="ts">
import ChildComponent from './ChildComponent.vue'
import { defineComponent, reactive } from 'vue'
// duplicate
enum Color {
red = 1,
blue
}
interface State {
color: Color
}
export default defineComponent({
components: {
ChildComponent
},
setup () {
const state = reactive<State>({
color: Color.red
})
return { state }
}
})
</script>