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

{{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"
      }
    ]
  }
];


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

最佳答案

是的,您可以通过像这样在路由文件中创建帮助器来实现

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 }}

关于javascript - 我如何在前端js变量中使用 Handlebars 数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54453660/

10-16 20:53