[英]i am trying to export a function to import into another javascript page but can not figure out how to do it with exports/imports
below is my code.下面是我的代码。 I just want to export the function cartUpdate() so i can import it into the index page.
我只想导出功能 cartUpdate() 以便我可以将其导入索引页面。 Any help is appreciated.
任何帮助表示赞赏。 Thank you!
谢谢!
function cartUpdate() {
let cartNum = 0
for (var i = 0, len = localStorage.length; i < len; ++i) {
let parsedCart = parseInt(localStorage.getItem(localStorage.key(i)))
cartNum += parsedCart
}
//display number of items in cart
const cartP = document.createElement('p')
cartP.innerHTML = cartNum
document.getElementById('cart-number').appendChild(cartP)
}
cartUpdate()
The export statement is used when creating JavaScript modules to export live bindings to functions, objects, or primitive values from the module so they can be used by other programs with the import statement. export 语句在创建 JavaScript 模块时用于从模块导出到函数、对象或原始值的实时绑定,以便其他程序可以使用 import 语句使用它们。 Bindings that are exported can still be modified locally;
导出的绑定仍然可以在本地修改; when imported, although they can only be read by the importing module the value updates whenever it is updated by the exporting module.
导入时,尽管它们只能由导入模块读取,但只要导出模块更新该值就会更新。
Exported modules are in strict mode whether you declare them as such or not.导出的模块处于严格模式,无论您是否声明它们。 The export statement cannot be used in embedded scripts.
不能在嵌入式脚本中使用 export 语句。
There are two types of exports:有两种类型的导出:
-Named Exports (Zero or more exports per module) -Default Exports (One per module) -命名导出(每个模块零个或多个导出) -默认导出(每个模块一个)
check out the Document : https://developer.mozilla.org/en-US/docs/web/javascript/reference/statements/export查看文档: https : //developer.mozilla.org/en-US/docs/web/javascript/reference/statements/export
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.