if(dataTemp.data=== 'undefined'){ }else{ message.open({ type: 'success', content: dataTemp.data.title+'下载完成', duration: 3, top: 200 }); } 这段代码无效
时间: 2024-02-10 07:05:44 浏览: 27
这段代码的问题在于`dataTemp.data=== 'undefined'`的判断方式是错误的。如果`dataTemp.data`不存在,它的值会是`undefined`,而不是字符串`'undefined'`。因此,你需要将判断条件修改为`typeof dataTemp.data === 'undefined'`,例如:
```
if(typeof dataTemp.data === 'undefined'){
// do something
} else {
message.open({
type: 'success',
content: dataTemp.data.title+'下载完成',
duration: 3,
top: 200
});
}
```
这样就可以正确地判断`dataTemp.data`是否存在了。
相关问题
typeof dataTemp.data.title === 'undefined' 报错 nnot read properties of undefined (reading 'title')
如果你的代码在执行`typeof dataTemp.data.title === 'undefined'`时报错`Cannot read properties of undefined (reading 'title')`,那么很可能是`dataTemp`或`dataTemp.data`本身为`undefined`。在这种情况下,你需要先判断`dataTemp`和`dataTemp.data`是否存在,例如:
```
if(dataTemp && dataTemp.data && typeof dataTemp.data.title === 'undefined'){
// do something
} else {
message.open({
type: 'success',
content: dataTemp.data.title+'下载完成',
duration: 3,
top: 200
});
}
```
这样就可以避免在`dataTemp`或`dataTemp.data`不存在时访问其属性导致的错误了。
if(dataTemp.data.title!=undefined){ message.open({ type: 'success', content: dataTemp.data.title+'下载完成', duration: 3, top: 200 }); } 这段代码判断无效,还是弹出提示
这段代码判断无效的原因可能是`dataTemp.data.title`的值不是`undefined`,而是一个非空字符串、数字或其他可转为真值的值。你可以试着添加一个`console.log(dataTemp.data.title)`语句,看一下`dataTemp.data.title`的实际值是什么。
如果你想要判断`dataTemp.data.title`是否为`undefined`,应该使用`typeof`运算符,例如:
```
if(typeof dataTemp.data.title === 'undefined'){
// do something
} else {
message.open({
type: 'success',
content: dataTemp.data.title+'下载完成',
duration: 3,
top: 200
});
}
```
这样就可以正确地判断`dataTemp.data.title`是否为`undefined`了。