朱凯
朱凯
  • 发布:2020-07-08 14:53
  • 更新:2020-07-22 11:56
  • 阅读:5908

【报Bug】编译错误 Cannot read property 'filter' of undefined

分类:uni-app

产品分类: uniapp/小程序/字节跳动

PC开发环境操作系统: Windows

PC开发环境操作系统版本号: win10

HBuilderX类型: 正式

HBuilderX版本号: 2.8.0

第三方开发者工具版本号: 1.9.4

基础库版本号: 1.67.0.3

项目创建方式: HBuilderX

操作步骤:
import Vue from "vue";  
import testC from "../components/test-c.vue";  
import { basePage } from "../common/basePage/basePage";  
const a = {  
  components: {  
    testC  
  },  
  data() {  
    return {};  
  },  
  methods: {}  
};  
export default Vue.extend(a);

预期结果:

编译正常, 组件正常

实际结果:

编译失败

bug描述:

在封装公共页面处理逻辑的时候, 发现页面如果不是直接导出对象就会导致编译报错, 报错如下

 ERROR  Failed to compile with 1 errors                                                                                                                                                                 14:23:14  
 error  in ./src/pages/test.vue?vue&type=script&lang=js&  

Module build failed (from ./node_modules/@dcloudio/webpack-uni-mp-loader/lib/script.js):  
TypeError: Cannot read property 'filter' of undefined  
    at handleObjectExpression (D:\Projects\saas-fe-xapp-ec-channel\multiple-uni\node_modules\@dcloudio\webpack-uni-mp-loader\lib\babel\scoped-component-traverse.js:26:53)  
    at ExportDefaultDeclaration (D:\Projects\saas-fe-xapp-ec-channel\multiple-uni\node_modules\@dcloudio\webpack-uni-mp-loader\lib\babel\scoped-component-traverse.js:90:11)  
    at NodePath._call (D:\Projects\saas-fe-xapp-ec-channel\multiple-uni\node_modules\@babel\traverse\lib\path\context.js:55:20)  
    at NodePath.call (D:\Projects\saas-fe-xapp-ec-channel\multiple-uni\node_modules\@babel\traverse\lib\path\context.js:42:17)  
    at NodePath.visit (D:\Projects\saas-fe-xapp-ec-channel\multiple-uni\node_modules\@babel\traverse\lib\path\context.js:90:31)  
    at TraversalContext.visitQueue (D:\Projects\saas-fe-xapp-ec-channel\multiple-uni\node_modules\@babel\traverse\lib\context.js:112:16)  
    at TraversalContext.visitMultiple (D:\Projects\saas-fe-xapp-ec-channel\multiple-uni\node_modules\@babel\traverse\lib\context.js:79:17)  
    at TraversalContext.visit (D:\Projects\saas-fe-xapp-ec-channel\multiple-uni\node_modules\@babel\traverse\lib\context.js:138:19)  
    at Function.traverse.node (D:\Projects\saas-fe-xapp-ec-channel\multiple-uni\node_modules\@babel\traverse\lib\index.js:84:17)  
    at NodePath.visit (D:\Projects\saas-fe-xapp-ec-channel\multiple-uni\node_modules\@babel\traverse\lib\path\context.js:97:18)  
    at TraversalContext.visitQueue (D:\Projects\saas-fe-xapp-ec-channel\multiple-uni\node_modules\@babel\traverse\lib\context.js:112:16)  
    at TraversalContext.visitSingle (D:\Projects\saas-fe-xapp-ec-channel\multiple-uni\node_modules\@babel\traverse\lib\context.js:84:19)  
    at TraversalContext.visit (D:\Projects\saas-fe-xapp-ec-channel\multiple-uni\node_modules\@babel\traverse\lib\context.js:140:19)  
    at Function.traverse.node (D:\Projects\saas-fe-xapp-ec-channel\multiple-uni\node_modules\@babel\traverse\lib\index.js:84:17)  
    at traverse (D:\Projects\saas-fe-xapp-ec-channel\multiple-uni\node_modules\@babel\traverse\lib\index.js:66:12)  
    at module.exports (D:\Projects\saas-fe-xapp-ec-channel\multiple-uni\node_modules\@dcloudio\webpack-uni-mp-loader\lib\babel\scoped-component-traverse.js:54:3)  

 @ ./src/pages/test.vue?vue&type=script&lang=js& 1:0-434 1:450-453 1:455-886 1:455-886  
 @ ./src/pages/test.vue

代码如下

import Vue from "vue";  
import testC from "../components/test-c.vue";  
import { basePage } from "../common/basePage/basePage";  
const a = {  
  components: {  
    testC  
  },  
  data() {  
    return {};  
  },  
  methods: {}  
};  
export default Vue.extend(a);

甚至这样也不行

import Vue from "vue";  
import testC from "../components/test-c.vue";  
import { basePage } from "../common/basePage/basePage";  
const a = {  
  components: {  
    testC  
  },  
  data() {  
    return {};  
  },  
  methods: {}  
};  
export default a;

只有下面这样直接导出对象,或者Vue.extend({...})才可以

import Vue from "vue";  
import testC from "../components/test-c.vue";  
import { basePage } from "../common/basePage/basePage";  
export default Vue.extend( {  
  components: {  
    testC  
  },  
  data() {  
    return {};  
  },  
  methods: {}  
});
2020-07-08 14:53 负责人:DCloud_UNI_GSQ 分享
已邀请:
DCloud_UNI_GSQ

DCloud_UNI_GSQ

bug确认,已经加分,后续优化

  • 朱凯 (作者)

    有修复的时间吗, 这边项目等待上线, 比较急

    2020-07-09 10:26

9***@qq.com

9***@qq.com - 天堂里的花大咩

其他uni-app都能优化,components部分它优化不了,这个是要静态编译成usingComponents的,字面量的描述components即可,拉出来描述

import Vue from "vue";  
    import testC from "../components/test-c.vue";  
    import { basePage } from "../common/basePage/basePage";  
    export default Vue.extend({  
        ...basePage({  
            data() {  
                return {};  
            },  
            methods: {}  
        }),  
        components: {  
            testC  
        },  
    });
  • DCloud_UNI_GSQ

    已对贴主提出的情况做了有限支持,不过其他复杂写法暂未处理

    2020-07-10 11:13

DCloud_UNI_GSQ

DCloud_UNI_GSQ

HBuilderX 更新之前可采取临时解决方案,替换 babel-plugin-scoped-component.js 到如下路径 HBuilderX.app/Contents/HBuilderX/plugins/uniapp-cli/node_modules/@dcloudio/webpack-uni-mp-loader/lib/babel-plugin-scoped-component.js
文件见附件

DCloud_UNI_GSQ

DCloud_UNI_GSQ

HBuilderX 2.8.1 alpha 已修复

该问题目前已经被锁定, 无法添加新回复