我如何在前端js变量中使用把手数据

时间:2019-01-31 05:07:34

标签: javascript node.js express handlebars.js

我正在将对象数组传递给车把。 我这样在前端检索

{{this.coment}}

对象包含以下内容:

`{
    _id: "5c527707e28b4fac758cc674",
    sectionId: "2",
    comments: [
      {
        replies: [],
        authorAvatarUrl:
          "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRDW3D0Emu0_gpP-tAEGPjW88zSabvpdICv6BaoNLArqY3xB4NA",
        authorName: "Jesus Zuñiga Vega",
        authorId: "1",
        authorUrl: "",
        comment: "Test",
        id: "72845"
      }
    ]
  },
  {
    _id: "5c5279f2e28b4fac758cc761",
    sectionId: "3",
    comments: [
      {
        replies: [],
        authorAvatarUrl:
          "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRDW3D0Emu0_gpP-tAEGPjW88zSabvpdICv6BaoNLArqY3xB4NA",
        authorName: "Jesus Zuñiga Vega",
        authorId: "1",
        authorUrl: "",
        comment: "Test2",
        id: "37940"
      }
    ]
  },
  {
    _id: "5c527ce2e28b4fac758cc887",
    sectionId: "1",
    comments: [
      {
        replies: [],
        authorAvatarUrl:
          "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRDW3D0Emu0_gpP-tAEGPjW88zSabvpdICv6BaoNLArqY3xB4NA",
        authorName: "Jesus Zuñiga Vega",
        authorId: "1",
        authorUrl: "",
        comment: "Hello",
        id: "77251"
      }
    ]
  }`

请注意,它缺少数组的“ []”

我想知道是否可以将数据放在js变量中,以便像这样在前端使用它:

var existingComments = [
  {
    _id: "5c527707e28b4fac758cc674",
    sectionId: "2",
    comments: [
      {
        replies: [],
        authorAvatarUrl:
          "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRDW3D0Emu0_gpP-tAEGPjW88zSabvpdICv6BaoNLArqY3xB4NA",
        authorName: "Jesus Zuñiga Vega",
        authorId: "1",
        authorUrl: "",
        comment: "Test",
        id: "72845"
      }
    ]
  },
  {
    _id: "5c5279f2e28b4fac758cc761",
    sectionId: "3",
    comments: [
      {
        replies: [],
        authorAvatarUrl:
          "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRDW3D0Emu0_gpP-tAEGPjW88zSabvpdICv6BaoNLArqY3xB4NA",
        authorName: "Jesus Zuñiga Vega",
        authorId: "1",
        authorUrl: "",
        comment: "Test2",
        id: "37940"
      }
    ]
  },
  {
    _id: "5c527ce2e28b4fac758cc887",
    sectionId: "1",
    comments: [
      {
        replies: [],
        authorAvatarUrl:
          "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRDW3D0Emu0_gpP-tAEGPjW88zSabvpdICv6BaoNLArqY3xB4NA",
        authorName: "Jesus Zuñiga Vega",
        authorId: "1",
        authorUrl: "",
        comment: "Hello",
        id: "77251"
      }
    ]
  }
];

非常感谢您的任何帮助!!!

2 个答案:

答案 0 :(得分:1)

一种实现此目的的方法是生成一个Uncaught TypeError: Cannot set property 'bootstrap' of undefined at eval (bootstrap.bundle.min.js?6518:7) 标签。

context="{'order_display': 'name desc' }"

现在,该变量应该可以在Javascript中使用。但是,我不喜欢这种方法,因为它使用的是全局变量。

答案 1 :(得分:1)

是的,您可以通过在这样的路由文件中创建帮助程序来实现此目的

handlebars = handlebars.create({
 helpers: {
  assign:function (varName, varValue, options) {
        if (!options.data.root) {
            options.data.root = {};
        }
        options.data.root[varName] = varValue;
    }
 }
})

然后在前端,您可以初始化或将值分配给这样的变量

{{assign 'existingComments' this.coment}}

然后才能访问可以使用

{{@root.existingComments }}