简体   繁体   English

如何让脚本在Jade中跨越多行?

[英]How can I let scripts span multiple lines in Jade?

Right now, I have this in a navigation view partial: 现在,我在导航视图中有这个部分:

script.
    links = [
        {title: 'Dashboard', url: '/'},
        { title: 'Users', sublinks: [
                {title: 'Manage', url: '/users'}
            ] }
    ]
ul.nano-content
    - each link in links
        li
            a(href="#")= link.title

But Jade is complaining, I'm getting this error: 但是Jade抱怨,我收到了这个错误:

Cannot read property 'length' of undefined

and it is pointing to - each link in links . 它指向- each link in links This works when I put everything on one line, but it's ugly and is tough to read/maintain, how can I make it possible to span multiple lines like I have it? 当我将所有内容放在一行上时,这种方法很有效,但它很丑陋且难以阅读/维护,我怎样才能跨越多条线路,就像我拥有它一样?

You're mixing two pieces of JavaScript - in-browser code you keep in between <script> tags, and backend JS. 您将两个JavaScript - 浏览器内代码混合在<script>标记和后端JS中。 Contents of script. script.内容script. ( <script/> tag) aren't bound to variables used in Jade template - so links in this line: <script/>标签)未绑定到Jade模板中使用的变量 - 因此此行中的links

    - each link in links

is undefined - a direct cause of your error. 未定义 - 导致错误的直接原因。

Solution depends on how you compile Jade template. 解决方案取决于您如何编译Jade模板。 Compilation is a process when as input you put both template itself and some data which will be bound to variables inside of template (ie links variable), and as an output you get HTML. 编译是一个过程,当你输入模板本身和一些数据时,它们将被绑定到模板内的变量(即links变量),并作为输出获得HTML。

For instance, if you're serving it dynamically from Node.js backend, assuming you're using Express.js, you might want to define route similar to: 例如,如果您从Node.js后端动态提供它,假设您正在使用Express.js,您可能希望定义类似于以下的路由:

app.get('/', function (req, res) {
    // .render() will take template named "index",
    // and a map with "links" property, and use it
    // to generate output HTML document.
    // Each key from the map will be bound at the template
    // level, so you'll be able to use it there.
    res.render('index', {links: [
        {title: 'Dashboard', url: '/'},
        { title: 'Users', sublinks: [
            {title: 'Manage', url: '/users'}
        ]}
    ]});
})

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

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