我传递一个对象数组到车把。我正在检索它像这样前端
{{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 }}
你可以做到这一点的方法之一是通过生成script
标签。
<script type="application/javascript">var comment = {{{this.coment}}};</script>
现在,该变量应该可以在你的JavaScript中使用。然而,由于它使用的是全局变量,我不喜欢这种做法。