简体   繁体   English

我正在尝试导出一个函数以导入到另一个 javascript 页面,但无法弄清楚如何使用导出/导入来做到这一点

[英]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.

相关问题 我是否正确使用withTracker? 我收到与进口/出口有关的错误,但无法找出问题 - Am I using withTracker correctly? I am getting an error relating to imports/exports but cannot figure out the issue 我正在尝试创建XML和XSL,但它给我一个错误,我不知道该怎么办 - I am trying to create an XML and XSL, but it is giving me an error that I can't figure out what to do 我正在尝试找出此Javascript代码出了什么问题 - I am trying to figure out what is wrong with this Javascript code 我想弄清楚下面的 javascript 以及它的作用 - I am trying to figure out below javascript and what it does 如何确定javascript中引用了哪个函数? - How can I figure out which function is being referenced in javascript? 我试图弄清楚如何让我的函数一个接一个地运行……但是它不起作用 - I am trying to figure out how to make my functions run one after another… but it is not working 我试图弄清楚如何更改页面上显示的日期格式 - I am trying to figure out how to change the format of the date displayed on my page 我想弄清楚如何使用 javascript 将字符串中的列转换为行 - I am trying to figure out how to convert columns in a string to row using javascript 如何通过 module.exports 导出这个名为 function 的文件? - How do I export this named function through module.exports? 我正在尝试制作HSV颜色选择器,但是我不知道如何生成重叠图像 - I am trying to make an HSV color picker but I can't figure out how to generate the overlay image
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM