我如何在Pug中的脚本标签中包含一个变量的代码块?

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

我有这个帕格犬混血儿。

mixin myMixin(options)
    - if (!options) options = {}
    script.
        var myObject = {
            "page": {
                "id": "",
                "title": document.title,
                "url": location.href,
                "breadcrumb": options.breadcrumb ? options.breadcrumb : [],
                "timestamp": new Date()
            }
        }

这就是它的编译结果

<script>
    var myObject = {
        "page": {
            "id": "",
            "title": document.title,
            "url": location.href,
            "breadcrumb": options.breadcrumb ? options.breadcrumb : [],
            "timestamp": new Date()
        }
    }
</script>

我不知道该如何让Pug编译的 "option.breadcrumb "变量工作。我希望编译后的代码能有option.breadcrumb属性的值或一个空数组。

任何帮助都是非常感激的!我有这个Pug mixin。

javascript pug
1个回答
0
投票

你必须使用插值法

<script>
    var breadcrumb = '!{options.breadcrumb}';

    var myObject = {
        "page": {
            "id": "",
            "title": document.title,
            "url": location.href,
            "breadcrumb": breadcrumb ? breadcrumb : [],
            "timestamp": new Date()
        }
    }
</script>
© www.soinside.com 2019 - 2024. All rights reserved.