三元表达式如何绑定数据嵌套?

发布于 6 年前作者 hanping5771 次浏览最后编辑 6 年前来自 issues

4 回复
hsu
hsu1 楼5 年前

写个wxs,用个switch判断多方便

wei59
wei592 楼5 年前
// 如果数据不多的话,考虑下下面这种写法吧
const grade = 1 || '2'
const gradeTxtList = ['无','无','白金','砖石']
const gradeTxt =  gradeTxtList[+grade||0]
// const gradeTxt  = !isNaN(+grade) ? gradeTxtList[+grade||0] :'default value'

// const gradeTxt =  ['无','无','白金','砖石'][+grade||0]

// const getGradeTxt = grade => ['无','无','白金','砖石'][+grade||0];

fxiong
fxiong3 楼5 年前
{{list.teamGrade == 0?'无':list.teamGrade == 1?'无':list.teamGrade == 2?'白金':'钻石'}}
teamGrade 为 0 等级为 无
teamGrade 为 1 等级为 无
teamGrade 为 2 等级为 白金
teamGrade 为 3及以上 等级为 钻石
yejie
yejie4 楼4 年前

建议写成{{tramGradeMap[list.teamGrade]}}

然后在js文件里定义好

tramGradeMap: {

0: ‘无’,

1: ‘无’,

2: ‘白金’,

3: ‘钻石’,

}

这样有再多的grade也不怕了

而且也更直观,

或者使用wxs的函数进行处理,更简便