在此输入代码
我正在创建一个在线目录,每次单击“更多信息”按钮时,我都会收到 undefined
replace
我尝试重命名 property
但它仍然无法正常工作
let removeCom = this.props.itemInfo.description.replace("?", "").replace('<td width="110" height="">', "").replace("http://extranet.acetools.com/Catalog/","assets/img/items/").replace((/<INPUT[^>]*>/gmi), "").split('<CENTER><FONT COLOR="RED">', 1);
console.log(removeCom);
最佳答案
这意味着两件事
要么 itemInfo 没有描述,要么你有拼写错误,因为当 javascript 找不到它返回的对象时 undefined 所以你试图访问未定义的替换方法,例如 undefined.replace( )
因此你的代码变成这样
let removeCom = this.props.itemInfo.undefined.replace("?", "").replace('<td width="110" height="">', "").replace("http://extranet.acetools.com/Catalog/","assets/img/items/").replace((/<INPUT[^>]*>/gmi), "").split('<CENTER><FONT COLOR="RED">', 1)
console.log(removeCom);
其他更安全的方法你可以这样做
let { itemInfo } = this.props;
let removeCom;
if(itemInfo.description){
removeCom = itemInfo.description.replace("?", "").replace('<td width="110" height="">', "").replace("http://extranet.acetools.com/Catalog/","assets/img/items/").replace((/<INPUT[^>]*>/gmi), "").split('<CENTER><FONT COLOR="RED">', 1)
console.log(removeCom);
}
关于javascript - 无法读取未定义的属性 "replace",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57380702/