最近在写一个项目的迁移,从angular迁移到Vue,angular中基本都是ts的用法,迁移到vue因为用的是vue2,暂时还不支持ts,所以代码从ts要修改成js,当中有些是ts的枚举enum的用法,但是为了代码字段名称的可读性需要作转换。
ts:
export enum userPage{
record,
upload,
paper,
paperCollect,
topic,
PaperEdit
}
修改后的js:
const userPage = {
record: 0,
upload: 1,
paper: 2,
paperCollect: 3,
topic: 4,
PaperEdit: 5
}
export{
userPage
}
然后他们用起来是这样的:
var paper= userPage.paper;
如果你想让枚举值持有属性,你可以把他们加到一个额外的属性上面,举另外一个例子:
var myEnum = {
SMALL: 1,
MEDIUM: 2,
LARGE: 3,
properties: {
1: {name: "small", value: 1, code: "S"},
2: {name: "medium", value: 2, code: "M"},
3: {name: "large", value: 3, code: "L"}
}
};
他们用起来像这样:
var mySize = myEnum.MEDIUM;
var myCode = myEnum.properties[mySize].code; // myCode == "M"