最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Migrating es5 to es6 export default - Stack Overflow

programmeradmin1浏览0评论

I am trying to migrate a code from es5 to es6, I am quite new in both, if someone could help me, I will be very thankful.

es5 version:

lib.js

module.exports = {
    foo1: function () { 
        this.foo2() {
           ... 
        }
    },
    foo2: function () { 
        ...
    }
}

main.js

const Lib = require("./lib");
Lib.foo1( { ... });

es6 version - I am trying:

lib.ts

export default { 
    foo1() {
        this.foo2(() => {
            ... 
        });                 
    },
    foo2(){ ... }
}

main.ts

import * as Lib from "./lib";
Lib.foo1({ ... })

The problem is in my main.ts foo1 can not be resolved. Any idea or remendation?

Thank!

I am trying to migrate a code from es5 to es6, I am quite new in both, if someone could help me, I will be very thankful.

es5 version:

lib.js

module.exports = {
    foo1: function () { 
        this.foo2() {
           ... 
        }
    },
    foo2: function () { 
        ...
    }
}

main.js

const Lib = require("./lib");
Lib.foo1( { ... });

es6 version - I am trying:

lib.ts

export default { 
    foo1() {
        this.foo2(() => {
            ... 
        });                 
    },
    foo2(){ ... }
}

main.ts

import * as Lib from "./lib";
Lib.foo1({ ... })

The problem is in my main.ts foo1 can not be resolved. Any idea or remendation?

Thank!

Share Improve this question asked May 9, 2017 at 9:49 KelyaneKelyane 4672 gold badges7 silver badges19 bronze badges 1
  • Try import Lib from "./lib"; – dfsq Commented May 9, 2017 at 9:50
Add a ment  | 

2 Answers 2

Reset to default 2

It should be just

import Lib from "./lib";

Otherwise, if you use * as notation you could access default export with Lib.default, but this is unnecessary.

I don't understand the following part of your code:

foo1: function () { 
    this.foo2() {
       ... 
    }
}

That seems invalid.

Anyway, do not introduce your own pseudo-module like structure. It's not necessary. lib.js is already a module.

lib.js

export function foo1() {
    foo2();
}
export function foo2() { ... }

main.js

import {foo, foo2} from './lib';
发布评论

评论列表(0)

  1. 暂无评论