如何调用外部文件中的JavaScript功能从EJS模板?

问题描述 投票:0回答:1

我有这个文件,添加一个新的船舶和脚本来填充德舰队下拉菜单正常工作:

new.ejs文件:

<% include ../partials/header %>

<div class="container">
    <div class="row">
        <h1 style="text-align: center;">Create a new Ship</h1>
        <div style="width: 30%; margin: 25px auto;">
            <form action="/ships" method="POST">
                <div class="form-group">
                    <input class="form-control" type="text" name="name" placeholder="name">
                </div>
                <div class="form-group">
                    <input class="form-control" type="number" name="tons" placeholder="tons" min="0" step="1">
                </div>
                <div class="form-group">
                    <input class="form-control" type="text" name="image" placeholder="img url">
                </div>
                <!--<div class="form-group">-->
                <!--    <input class="form-control" type="text" name="fleet" placeholder="fleet">-->
                <!--</div>-->

                <div class="form-group">
                    <select class="form-control" type="text" name="fleet" id="selectNumber" placeholder="fleet">
                        <option>Choose a fleet</option>
                    </select>
                </div>

                <script>
                    var select = document.getElementById("selectNumber");
                    var options = ["First fleet", "Second fleet", "Metropolitan fleet", "Support fleet"];
                    for(var i = 0; i < options.length; i++) {
                        var opt = options[i];
                        var el = document.createElement("option");
                        el.textContent = opt;
                        el.value = opt;
                        select.appendChild(el);
                    }
                </script>




                <div class="form-group">
                    <input class="form-control" type="text" name="description" placeholder="description">
                </div>
                <div class="form-group">
                    <button class="btn btn-lg btn-primary btn-block">Submit!</button>
                </div>
            </form>
            <a href="/ships/">Back</a>
        </div>
    </div>

</div>


<% include ../partials/footer %>

我想从视图分离的“逻辑”,所以我需要创建一个文件:../public/js/jsscripts.js

我猜jsscripts.js内

module.exports = {
  PopulateFleet: function () {
var select = document.getElementById("selectNumber");
                    var options = ["First fleet", "Second fleet", "Metropolitan fleet", "Support fleet"];
                    for(var i = 0; i < options.length; i++) {
                        var opt = options[i];
                        var el = document.createElement("option");
                        el.textContent = opt;
                        el.value = opt;
                        select.appendChild(el);
                    }

};

1)我需要导入该文件“jsscripts”从我app.js?就像是:

jscripts= require ("./public/js/jscripts");

2)如何调用从new.ejs功能“PopulateFleet”文件?就像是??

<% PopulateFleet %>

要么

<% jscripts.PopulateFleet %>

文件夹结构

舰队 - 公众 - JS - > jsscripts.js

舰队 - 视图 - 船 - > - new.ejs

javascript node.js express ejs
1个回答
0
投票

use <%= jscripts.PopulateFleet() %>

© www.soinside.com 2019 - 2024. All rights reserved.