enum AnimalEnum {
Dog,
Cat,
Tiger
}
const animalList = Object.values(AnimalEnum).filter(value => typeof value === 'string')
.map(value => ({ name: String(value), value: AnimalEnum[value] }) );
console.log(animalList);
// [ {name: "Dog", value: 0},{name: "Cat", value: 1},{name: "Tiger", value: 2} ]
從 html 的 $event 傳到 ts 的值假如是 string 型別,會對應不到 enum 輸出的值,因為 enum 輸出的值 是 number 要注意一下
switch(myAnimal) {
case AnimalEnum.Dog:
console.log('Dog');
break;
case AnimalEnum.Cat:
console.log('Cat');
break;
case AnimalEnum.Tiger:
console.log('Tiger');
break;
}