简体   繁体   English

为什么将函数导入另一个文件时函数的属性不可见?

[英]Why property of the function invisible when function imported to another file?

I have this function in file colled dataservice.js:我在文件 colled dataservice.js 中有这个功能:

export function DataServices() {
    var that = this;
    that.dataServiceUrl = "/CrossServices/SearchService.svc";

    var ds = {
        getStreets: getStreets
    };
    return ds;

    function getStreets(expr, callback, error) {
        //some logic
        return result;
    }
}

I import function above in mapApp.js file like this:我在 mapApp.js 文件中导入上面的函数,如下所示:

    import { DataServices  from "/src/js/services/dataservices";

And try to access getStreets property this way:并尝试以这种方式访问​​ getStreets 属性:

        DataServices.getStreets(3, null, null);

But I get this error on the row above:但是我在上面的行中收到此错误:

error:  Cannot read property 'getStreets' of undefined  

Any idea why getStreets property not visible?知道为什么 getStreets 属性不可见吗?

You are exporting a named function DataServices您正在导出命名函数DataServices

You need to make it the default export, or import it by name:您需要将其设为默认导出,或按名称导入:

export default function DataServices

or或者

import { DataServices } from "/src/js/services/dataservices";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM