Import properties from an object

前端 未结 2 1545
情深已故
情深已故 2021-01-14 00:50

I am trying to import a single function from a functions file. The functions file looks like this.

const Functions = {
    url(path = \'\') {
        path =          


        
相关标签:
2条回答
  • 2021-01-14 01:17

    What you have done - is exported an object.

    In that case you need to import an object and access its property:

    import Functions from "../Utils/Functions";
    Functions.url();
    

    If you want to make named export - you need to change the way you're exporting and defining it:

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

    or

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

    As another note: it is not destructuring, even though it looks similar. The standard names it as ImportsList and defines its own semantic, different from the destructuring one.

    References:

    • https://developer.mozilla.org/en/docs/web/javascript/reference/statements/export
    • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
    • http://www.ecma-international.org/ecma-262/6.0/#sec-imports
    0 讨论(0)
  • 2021-01-14 01:30

    If you use 'default export' then the import should be:

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

    Actually, you can import it with any identifier you like (not only 'Functions')

    0 讨论(0)
提交回复
热议问题