最基础的三元运算符写法
在行间最基础的写法
{{ a=1?'你好':'Hello'}}
但是在开发中难免会遇到比较复杂的需求,如果要是在script标签里面写,可能会需要很多逻辑,这个时候要是写三元运算符可能一行就解决了,看个人喜好了
script里面的逻辑
this.percentageListBox.map((item) => {
if (item.grouplabel === "0%") {
item.taskStatusDisplay = "未开始";
} else if (item.grouplabel === "100%") {
item.taskStatusDisplay = "已完成";
} else {
item.taskStatusDisplay = "进行中";
}
});
在行间使用三元运算符的写法,就这一行
item0.fulfillPercent === "0%" ? '未开始': item0.fulfillPercent === "100%"?'已完成':'进行中'
下面记录一下,别的大佬写的
hour < 8? '早上好': hour <= 11 ? '上午好': hour <= 13? '中午好': hour < 18? '下午好': '晚上好'
const thisTime =
hour < 8? '早上好': hour <= 11 ? '上午好': hour <= 13? '中午好': hour < 18? '下午好': '晚上好'
const hour = new Date().getHours()
const thisTime =
hour < 8? '早上好': hour <= 11 ? '上午好': hour <= 13? '中午好': hour < 18? '下午好': '晚上好'
Vue.prototype.$baseNotify(`欢迎登录${title}`, `${thisTime}!`)