Javascript 从对象导入属性

Javascript 从对象导入属性,javascript,ecmascript-6,Javascript,Ecmascript 6,我正在尝试从函数文件导入单个函数。函数文件如下所示 const Functions = { url(path = '') { path = path.replace(/^\/+/, ''); return `${document.baseURI}/${path}`; }, asset(path = '') { return this.url(path); } }; export default Functions

我正在尝试从函数文件导入单个函数。函数文件如下所示

const Functions = {
    url(path = '') {
        path = path.replace(/^\/+/, '');
        return `${document.baseURI}/${path}`;
    },

    asset(path = '') {
        return this.url(path);
    }
};

export default Functions;
import {url} from "../Utils/Functions";
然后我尝试像这样导入
url
函数

const Functions = {
    url(path = '') {
        path = path.replace(/^\/+/, '');
        return `${document.baseURI}/${path}`;
    },

    asset(path = '') {
        return this.url(path);
    }
};

export default Functions;
import {url} from "../Utils/Functions";
当我这样做时,我从browserify浏览器中得到以下错误

未捕获类型错误:(0,_Functions.url)不是函数

根据文档,此导入应在
函数
对象中作为
url
工作


我做错了什么?

您所做的-被导出为一个对象

在这种情况下,需要导入对象并访问其属性:

import Functions from "../Utils/Functions";
Functions.url();
如果要进行命名导出-需要更改导出和定义的方式:

function url(path = '') {
    path = path.replace(/^\/+/, '');
    return `${document.baseURI}/${path}`;
}

function asset(path = '') {
    return this.url(path);
}

export { url, asset };

另一个注意事项是:它不是解构,尽管看起来很相似。标准将其命名为
ImportsList
,并定义了自己的语义,不同于解构语义

参考资料:


如果使用“默认导出”,则导入应为:

import Functions from "../Utils/Functions";

实际上,您可以使用您喜欢的任何标识符导入它(不仅仅是“函数”)

从“./Utils/Functions”导入url
对您有效我错误地认为可以在导入的对象上使用解构。@问题是-它不是解构,尽管看起来有点类似。