: Unexpected token u in JSON at position错误

: Unexpected token u in JSON at position错误
这是因为localStorage.getItem("cart")获取的值是undefined
解决方法
var cart= localStorage.getItem("cart");
if(Object.keys(cart)){
var cartlist = JSON.parse(localStorage.getItem("cart")) || [];
}else{
var cartlist=[]
}

你可能感兴趣的:(: Unexpected token u in JSON at position错误)