【问题标题】:Pug - pass json data from pug to pugPug - 将 json 数据从 pug 传递到 pug
【发布时间】:2017-10-06 10:17:26
【问题描述】:

我想做的是传递一个 json 数据并在其他哈巴狗中循环,但是出现了一些错误。

哈巴狗 1

-
  var json = [
    {type:"2", shareTools:"true", id:12},
    {type:"1", shareTools:"false", id:20}
  ]
include pug2.pug

哈巴狗 2

div.json #{json}
each data in json
    li= data.type

这里是 div.json #{json}的html输出

[object Object],[object Object]

gulp 错误信息

> 4| each data in json
Cannot read property 'length' of undefined

谢谢。

【问题讨论】:

  • 如果您在div.json 之后添加此行,您会在控制台中得到什么:- console.log( json );
  • 输出json数据,[ {type:"2", shareTools:"true", id:12}, {type:"1", shareTools:"false", id:20} ]
  • 也许,有一个案例问题,即获取 JSON 本机对象,尝试重命名您的变量。还要尝试始终使用语义变量名称而不是技术名称。

标签: json object pug


【解决方案1】:

您好,希望对您有所帮助

更改了 pug2 中的第二行。

哈巴狗1

-
    var jsonnn = [
            {type:"2", shareTools:"true",
            id:12},{type:"1", shareTools:"false", id:20}
        ]

include pug2

哈巴狗2

div.json #{json}
each data, i in jsonnn
    li= data.type

【讨论】:

    【解决方案2】:

    从风格的角度来看,mixin 可能值得考虑。

    哈巴狗1:

    include mixin1
    -
        var data_set = [
                {type:"2", shareTools:"true",
                id:12},{type:"1", shareTools:"false", id:20}
            ]
    mixin1(data_set)
    

    Pug 2 是 mixin:

    mixin mixin1(data_set)
    each data in data_set
        li= data.type
    

    【讨论】:

    • 发现一个错字:在 Pug1 中对 mixin1(data_set) 的调用应该是 +mixin1(data_set)。此外,将 mixin 存储在单独的文件夹中也是一种很好的形式。
    猜你喜欢
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-15
    • 2017-06-18
    • 1970-01-01
    相关资源
    最近更新 更多