Does import create a new copy of imported library?

风流意气都作罢 提交于 2020-01-01 10:12:03

问题


I'm using webpack + vue-loader to create vuejs app. I have multiple .vue files for components. When I write something like this:

import _ from 'lodash'

inside the script part of ComponentA.vue and ComponentB.vue, does this create two separate copies of lodash or does it simply import a reference?


回答1:


Importing an ES6 module, or any part of an ES6 module, produces a binding.

CommonJS modules export values, while ES6 modules export immutable bindings. This blog post explains what that means.

[ Source: ES6 Module Exports ]

So the answer is no, it does not create a copy of the exports. The module is initialised once and each import will receive a reference to the same value.



来源:https://stackoverflow.com/questions/37965902/does-import-create-a-new-copy-of-imported-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!