vue-element-admin修改stor/modules/permission.js
就这个位置
if (roles.includes('admin')) {
accessedRoutes = asyncRoutes || []
} else {
accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
}
意思是admin这个角色能访问所有的路由,就稍微给修改下,admin的角色也只能访问roles为admin的路由
if (roles.includes('admin')) {
// accessedRoutes = asyncRoutes || []
var arr = []
asyncRoutes.forEach((item) => {
if (Object.prototype.hasOwnProperty.call(item, 'meta')) {
if (Object.prototype.hasOwnProperty.call(item.meta, 'roles')) {
if (item.meta.roles.includes('admin')) {
arr.push(item)
}
}
}
})
accessedRoutes = arr || []
} else {
accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
}
最初Object.prototype.hasOwnProperty.call(item, 'meta')这个地方照着网络视频教材用的是
item..hasOwnProperty('meta')
但是提示这样的错误
Do not access Object.prototype method 'hasOwnProperty' from target object
通过上述文字提示发现,不要从目标对象访问 Object 原型方法”,想到一种解决方案——直接找到这个方法,用 call 改变指向调用:
Object.prototype.hasOwnProperty.call(obj, 'key')
1
在ECMAScript 5.1中,新增了 Object.create,它支持使用指定的 [[Prototype]] 创建对象。Object.create(null) 是一种常见的模式,用于创建将用作映射的对象。当假定对象将包含来自Object.prototype 的属性时,这可能会导致错误。该规则防止直接从一个对象调用某些 Object.prototype 的方法。
此外,对象可以具有属性,这些属性可以将 Object.prototype 的内建函数隐藏,可能导致意外行为或拒绝服务安全漏洞。例如,web 服务器解析来自客户机的 JSON 输入并直接在结果对象上调用 hasOwnProperty 是不安全的,因为恶意客户机可能发送一个JSON值,如 {“hasOwnProperty”: 1},并导致服务器崩溃。
为了避免这种细微的 bug,最好总是从 Object.prototype 调用这些方法。例如,foo.hasOwnProperty(“bar”) 应该替换为 Object.prototype.hasOwnProperty.call(foo, “bar”)。
错误解决参考 https://blog.csdn.net/qq_36727756/article/details/105464902
有好的文章希望朋友们帮助分享推广,猛戳这里河洛伊哥