bug解读:
错误信息 "Uncaught (in promise) TypeError: menu.forEach is not a function" 表示在 JavaScript 的异步代码块(可能是在 Promise
中或者使用 async/await
语法)中,尝试调用 menu
对象的 forEach
方法时发生了类型错误。这通常意味着 menu
并不是一个数组,或者它是 undefined
,因为只有数组才具有 forEach
方法。
分析与解决:
既然menu有可能不是数组,可以打印一下menu:
// 动态添加路由
addMenu(state, router){
console.log('router', router);
if(!Cookie.get('menu')) return
const menu = Cookie.get('menu')
console.log('menu', menu);
state.menu = menu
打印结果:
发现这是序列化后的数据。所以这是获取menu的时候忘记转成对象或者数组了。修改:
addMenu(state, router){
console.log('router', router);
if(!Cookie.get('menu')) return
const menu = JSON.parse(Cookie.get('menu'))
console.log('menu', menu);
打印结果:
这样bug就解决啦!!