Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

关于vue动态路由加载component,报了关于严格模式的问题

后台读取生成动态路由时,二级菜单注册component,一直无法成功。
image.png
如果component为Layout就可以成功。

错误:
TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them at Function.o

尝试关闭严格模式但是失败了。

代码:

export function addAsyncRouter(routers){
  const accessedRouters = routers.filter(router => {
    if (router.component) {
      if (router.component === 'Layout') {
        router.component = Layout
      } else {
        const component = router.component
        router.component = () => import('@/views/' + component)
      }
    }
    if (router.children && router.children.length) {
      router.children = filterAsyncRouter(router.children)
    }else{
      router.children = []
    }
    return true
  })
  return accessedRouters
}

感觉是这句的问题

router.component = () => import("@/views/" + component)

之前还试过

router.component = resolve => require(["@/views/" + component], resolve)

也还是一样的问题

想请教各位大佬,这个还有别的格式么


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

component函数中用到了caller、callee或arguments属性,这些属性在严格模式下是有限制的。

这和引入方法无关(import或require),优先推荐修改component函数,去掉里面的caller、callee或arguments属性,拥抱新特性、新规范,如果component函数不方便修改(如来自第三方插件),那么需要在webpack中配置移除严格模式(开发模式和生产模式都需要)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...