때때로 이 오류는 JSON.stringify()를 사용할 때 보고됩니다.
header.vue:92 포착되지 않음(약속 중) TypeError: 원형 구조를 JSON으로 변환
--> 생성자 'd'가 있는 개체에서 시작
| 속성 '_readableState' -> 생성자가 'b'인 객체
| 속성 '파이프' -> 생성자가 'i'인 객체
| 속성 '_readableState' -> 생성자 'b'가 있는 객체 --- 속성 '파이프'는 JSON.stringify(<anonymous>)에서 원
을 닫습니다.
해결책:
// 由于json.stringify嵌套循环导致报错的解决函数,obj是要stringify的对象
const circularSafeStringify = (obj) => {
const cache = new Set();
return JSON.stringify(obj, (key, value) => {
if (typeof value === "object" && value !== null) {
if (cache.has(value)) {
// 当前对象已经存在于缓存中,说明存在循环引用,返回占位符或其他处理方式
return "[Circular Reference]";
}
cache.add(value);
}
return value;
});
}