在找到this method个使用枚举来定义对象之后,我想在Vue中使用这样的对象.
export enum Options {
randSize = 'randomSized',
timer = 'showTimer',
randAngle = 'randomAngle',
colours = 'multiColour',
cardSize = 'cardSize',
numCards = 'numCards'
}
(主零部件):
import { Options} from './util'
let options : {[key in Options]: number | boolean} = {
[Options.randSize]: true,
[Options.timer] : false,
[Options.randAngle] : true,
[Options.colours] : true,
[Options.cardSize] : 8,
[Options.numCards] : 20
}
我还有一个改变期权价值的方法(感谢this post):
function changeOption( option: string, value: number | boolean) {
options = {...options,[option]:value}
}
但是,当我想要将这些选项用作组件属性时,无法识别枚举:
(父模板):
<game-options
:options = "options"
@done-setup = "gameStarted=true"
@change-options="changeOption"
>
</game-options>
(子组件):
import { ref, onMounted } from 'vue'
import { Options} from '../util'
const props = defineProps<{
options: {[key in Options]: number | boolean}
}>()
onMounted(() =>{
console.log(props.options);
let foo = props.options.randSize;
})
给出错误Property 'randSize' does not exist on type '{ randomSized: number | boolean; showTimer: number | boolean; randomAngle: number | boolean; multiColour: number | boolean; cardSize: number | boolean; numCards: number | boolean; }'.ts(2339)
如何纠正这一点?