javascript - 脚本。 JADE 标签未在 Express 中呈现

标签 javascript node.js express pug

我希望在我的 JADE 模板中包含客户端脚本并编写

扩展布局

script.
    function collect_data() {

        var transitions = {};

        $( ":checkbox:checked" ).each(function (index, element) {
        //// some code
    }

block content
    h1= title

    table
        - for (var i = 1; i < images.length; ++i) {
            - var image;
            // and so on

我期待类似的事情

<script>
    function collect_data() {

    var transitions = {};
    ...

</script>

在我的网页源代码中,但我没有看到它。

什么是不正确的?如何在 Jade 中将嵌入的 javascript 渲染到模板中?

最佳答案

您正在通过扩展布局

扩展布局

这意味着您处于子模板中,根据 pug 的 docs common mistakes section

Note that only named blocks and mixin definitions can appear at the top (unindented) level of a child template. This is important! Parent templates define a page’s overall structure, and child templates can only append, prepend, or replace specific blocks of markup and logic. If a child template tried to add content outside of a block, Pug would have no way of knowing where to put it in the final page.

这意味着你应该做类似的事情

//- layout.pug
html
  head
    block head
  body
    block content

并在子模板中执行类似的操作

extends layout

block append head
  script.
        function collect_data() {

            var transitions = {};

            $( ":checkbox:checked" ).each(function (index, element) {
            //// some code
        }

将使其出现在布局中的head部分下

关于javascript - 脚本。 JADE 标签未在 Express 中呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44575383/

相关文章:

javascript - 使用js的多个字符串操作

node.js - 如何为 sequelize 多对多关联方法创建类型安全,例如(setModels 等)

javascript - NodeJS 的 MongoDB 连接错误

express - 如何在express js中向客户端发送数据?

node.js - Node : How to catch an exception from middleware?

javascript - 使用 AngularJS 重定向到带有 url 参数的页面时出现 404

javascript - JQuery 表情符号选择器

javascript - 如果 json 请求返回下一个链接为 "next",则隐藏 "false"链接

node.js - .pipe() 在 gulp 中到底是什么意思?

mysql - 使用 Node.js 和 MySQL 进行两个查询和循环