简体   繁体   English

在车把模板中包含js模块

[英]include js module in handlebars template

I have the following files: 我有以下文件:

server.js: server.js:

app.get('/main/:id', function(req, res) {
    res.render('main', { products: products });
})

main.hbs: main.hbs:

<div class="products">
    {{#each products }}
        {{ this.product_id }}
        {{ this.product_name }}
    {{/each}}
</div>


<script>
$(document).ready(function() {
    $(".add-to-cart").click(function() {
        const products = {
            item_id: $(this).attr('item_id'),
            item_name: $(this).attr('item_name'),
        }

        shopping_cart = new ShoppingCart();
        shopping_cart.addtoCart(products);
    })
})
</script>

scripts/shopping_cart.js: 脚本/shopping_cart.js:

function ShoppingCart() {
    this.addtoCart = function(products) {
        //save products to server etc..
    }
}

module.exports = ShoppingCart;

The only problem i have is that i'm trying to include the shopping_cart script" in the main.hbs file without any success 我唯一的问题是我试图在main.hbs文件中包含shopping_cart脚本”但没有成功

at first i did in the main.hbs: 起初我在main.hbs中做了:

<script src="../scripts/shopping_cart.js"></script>

but than i got the error in the browser: "Uncaught ReferenceError: module is not defined" 但比我在浏览器中看到的错误:“未捕获的ReferenceError:模块未定义”

than i tried to webpack it into a bundle.js file: and i got no error but if i click the "add to cart" button i get: "Uncaught ReferenceError: ShoppingCart is not defined" 而不是我尝试将其打包到bundle.js文件中:并且没有错误,但是如果我单击“添加到购物车”按钮,我将得到:“未捕获的ReferenceError:ShoppingCart未定义”

Please help me to understand the right approach how to include the shopping_cart.js file and be able to create new instance of the class. 请帮助我了解正确的方法,该方法如何包含shopping_cart.js文件并能够创建该类的新实例。

thanks. 谢谢。

You're tripping over a very typical stumbling block that a lot of people trip over when starting out with frontend + backend js. 您遇到了一个非常典型的绊脚石,当人们开始使用前端+后端js时,很多人都会绊脚石。

module.exports is nodejs code, it will not work in the browser. module.exports是nodejs代码,它将无法在浏览器中运行。 You will need to write your shopping_cart.js in normal JavaScript without using nodejs functions and objects. 您将需要使用常规JavaScript编写shopping_cart.js ,而不使用nodejs函数和对象。 In this case you should be able to simply ommit the module.exports . 在这种情况下,您应该能够简单地省略module.exports But this feels like a bigger issue of confusion. 但这似乎是一个更大的混乱问题。

You should look into JavaScript and nodejs separately, especially if you are new to either/both. 您应该分别研究JavaScript和nodejs,特别是如果您对这两者都不熟悉。 While some components can be shared, it's a bit more complex than a simple module.exports sadly. 虽然可以共享某些组件,但它比简单的module.exports要复杂一些。 I would suggest looking into some vanilla frontend (browser) JavaScript tutorials first, as it has no concept of things like module.exports or require('...') . 我建议先研究一些香草的前端(浏览器)JavaScript教程,因为它没有诸如module.exportsrequire('...')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM