gpt4 book ai didi

javascript - 在字符串模板中使用三元运算符的正确方法是什么?

转载 作者:行者123 更新时间:2023-11-28 16:58:05 25 4
gpt4 key购买 nike

我有一个 getter,它必须根据现有的 props 返回不同的值。它看起来像:

const converter = ({ propsValue }) => {
return {
get label() {
return `${propsValue} ? ${propsValue} : ${anotherValue} ${secondAnotherValue}`
}
}
}

问题是:字符串模板文字中三元运算符的正确语法是什么?

最佳答案

您需要将条件放在括号中

const converter = ({ propsValue }) => {
return {
get label() {
return `${propsValue ? propsValue : 'anotherValue'}`
}
}
}

具有动态值(value)

return `${propsValue ? propsValue : anotherValue}`;

具有两个动态值

return propsValue ? propsValue : `${anotherValue} ${secondAnotherValue}`;

或者

return `${propsValue ? propsValue : anotherValue + ' ' + secondAnotherValue}`;

关于javascript - 在字符串模板中使用三元运算符的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58524501/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com