Vue计算属性中,一个数组对象使用map函数报错是什么情况?

vue组件中的一个计算属性code,依赖另一个计算属性projectPath(该属性只有数组或者undefined两种值),但是在code的计算函数中调用map,却报

TypeError: this.projectPath.map(...) is not a function

这是什么道理???

代码:

    code () {
      if (this.projectPath) {
        console.log(typeof this.projectPath.map)
        let list = this.projectPath.map(item => item.nodeCode)
        return list.join('_')
      }
      return undefined
    },

报错:

clipboard.png

阅读 5.8k
3 个回答

自己坑了自己半小时。在其他地方写了一个错误,导致vue不会自动更新,这里应该是旧的代码有问题,但是改正后没有更新,导致代码和报错对不上。

你的报错里说的是 pathList 而你打印输出的是 projectPath

你应该看 pathList 是不是 null 或者 undefined 了。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题