Javascript 如何将NPM包导入浏览器';s控制台
为了使用一些软件包函数,我想将其导入浏览器的控制台。我已经尝试过这种方法,但它给出了一个解析错误Javascript 如何将NPM包导入浏览器';s控制台,javascript,browser-console,Javascript,Browser Console,为了使用一些软件包函数,我想将其导入浏览器的控制台。我已经尝试过这种方法,但它给出了一个解析错误 var script = document.createElement('script'); script.type = 'text/javascript'; script.src = 'https://unpkg.com/libphonenumber-js@1.7.26/core/index.js'; document.head.appendChild(script); 我所犯的错误 parse
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://unpkg.com/libphonenumber-js@1.7.26/core/index.js';
document.head.appendChild(script);
我所犯的错误
parsePhoneNumberFromString.js:1未捕获的语法错误:无法使用
模块外的import语句
我正在尝试将parsePhoneNumberFromString
函数用于我的浏览器控制台。您不能使用
标记来导入这样的模块,或者如果您这样做,您可以在脚本本身内使用类似于import*as reduxSaga from的语句来导入模块"https://unpkg.com/redux-saga@1.0.3/dist/redux saga effects.esmodules browsers.js“
关键是在
标记上设置,以便type=“module”
知道如何将脚本作为模块加载(这允许导入导出
)
例如:
index.js
从导入{parsePhoneNumberFromString}”https://unpkg.com/libphonenumber-js@1.7.26/core/index.js”;
const pnStr=“555-555-5555”;
const pn=parsePhoneNumberFromString(pnStr);
控制台日志(pn);
index.html