${item.shelfName} 三元运算
时间: 2023-11-23 14:29:27 浏览: 36
使用三元运算符 `? :` 可以在模板字符串中根据条件来动态显示不同的文本内容。例如:
```
`当前货架名称为 ${item.shelfName ? item.shelfName : '未知'}`
```
在这个模板字符串中,`${}` 中使用了一个三元运算符。如果 `item.shelfName` 存在且有值,则显示 `item.shelfName` 的值;否则,显示字符串 `"未知"`。
这个模板字符串可以用于返回一个文本内容,以描述当前货架的名称。如果 `item.shelfName` 有值,则返回货架的名称;否则,返回一个默认的未知字符串。
相关问题
vue2判断this.$route.query.pageSize
在 Vue 2 中,你可以通过以下方式判断 `this.$route.query.pageSize` 是否存在:
```
if (this.$route.query && this.$route.query.pageSize) {
// 存在
} else {
// 不存在
}
```
这里首先需要判断 `$route.query` 是否存在,因为如果 `$route.query` 不存在,那么访问 `$route.query.pageSize` 会报错。如果 `$route.query` 存在,那么再判断 `pageSize` 是否存在即可。
另外,你也可以使用三元表达式简化代码:
```
const isPageSizeExist = this.$route.query && this.$route.query.pageSize ? true : false;
```
这里定义了一个变量 `isPageSizeExist`,如果 `$route.query` 以及 `pageSize` 存在,那么该变量的值为 `true`,否则为 `false`。
在data里定义的变量,三元运算符不应嵌套,应该怎么修改 selectValue: this.$route.query.made === 'isProduct' ? 'product' : this.$route.query.made === 'isServer' ? 'server' : 'project'
可以使用 if-else 语句来代替三元运算符的嵌套,例如:
```
if (this.$route.query.made === 'isProduct') {
selectValue = 'product';
} else if (this.$route.query.made === 'isServer') {
selectValue = 'server';
} else {
selectValue = 'project';
}
```
这样可以使代码更易读和维护。