beforeRouteEnter
(
to,
from
, next
) {
next
(
vm
=>
{
if
(
from
.
path
==
'/movie/chooseCinema'
||
from
.
path
==
'/movie/chooseCinema_wd'
) {
sessionStorage.
setItem
(
'chooseCinemaQuery'
,
JSON
.
stringify
(
from
);
一般报错
TypeError: Converting circular structure to JSON
是因为存在循环引用,并且使用
JSON.stringify
方法去转化成字符串。下面举一个简单地例子:
const x = { a: 8 };
const b = { x };
b.y = b;
JSON.stringify(b);
const x = { a: 8 };
const b = { x };
b.y = JSON.parse(JSON.stringify(b));
JSON.stringify(b);
使用JSON.parse(JSON.stringify(from)) 解决,还是报一样的错!!!
解决办法一
直接删除导致循环引用的代码
解决办法二
beforeRouteEnter(to, from, next) {
var cache = [];
var str = JSON.stringify(from, function(key, value) {
if (typeof value === 'object' && value !== null) {
if (cache.indexOf(value) !== -1) {
return;
cache.push(value);
return value;
cache = null;
next(vm => {
if (from.path == '/movie/chooseCinema' || from.path == '/movie/chooseCinema_wd') {
sessionStorage.setItem('chooseCinemaQuery', str);
问题的关键点是解除循环引用
# TypeError: Converting circular structure to JSON