Как я могу включить элемент script в шаблон Handlebars? - PullRequest
0 голосов
/ 22 января 2020

Итак, я пытаюсь создать файл JavaScript на стороне клиента для одного из моих шаблонов .hbs. Но, похоже, он даже не загружает файл. js, когда я добавляю тег в код html. Я искал решение, но не могу найти четкого ответа на руль, по крайней мере, не тот, который я могу понять.

Я пытался: указать type = "text / javascript", добавив тег в main.hbs прямо под {{{body}}}, чтобы посмотреть, работает ли он, но ничего не произошло. Игра с файлом. js, но он явно не выполняется.

booking-functions.hbs

console.log("test")
document.addEventListener("DOMContentLoaded", function(){
console.log("In bookingfunctions")

// bunch of code

})

booking.hbs


    <div>
     <script src="../functions/booking-functions.js"></script>
     // Bunch of html that works fine with handlebars
    </div>
    ```
    main.hbs
    ======
    ```
    <!DOCTYPE html>
    <html lang="en">
    <head>
     // Nothing javascript or handlebars related here
    </head>
    <body>
        // Navbarcode and such

        //Login check with #if handlebars

        {{{body}}}

    </body>
    </html>

app. js


    const path = require('path')
    const express = require('express')
    const expressHandlebars = require('express-handlebars')

    const variousRouter = require('./routers/various-router')
    const accountRouter = require('./routers/account-router')
    const bookingRouter = require('./routers/booking-router')


    const app = express() 

    //Session code here

    // Setup express-handlebars.
    app.set('views', path.join(__dirname, 'views'))

    app.engine('hbs', expressHandlebars({
        extname: 'hbs',
        defaultLayout: 'main',
        layoutsDir: path.join(__dirname, 'layouts')
    }))

    // Handle static files in the public folder.
    app.use(express.static(path.join(__dirname, 'public')))


    //body parser middleware
    app.use(express.json());
    app.use(express.urlencoded({extended:false}));

    // Attach all routers.
    app.use('/', variousRouter)
    app.use('/accounts', accountRouter)
    app.use('/booking', bookingRouter)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...