gpt4 book ai didi

javascript - HandlebarsJS 未捕获类型错误 : Cannot read property 'url' of undefined

转载 作者:行者123 更新时间:2023-11-30 06:26:07 25 4
gpt4 key购买 nike

我刚刚开始学习 Handlebars.js,我使用 Handlebars.js 网站 ( http://handlebarsjs.com/expressions.html ) 帮助在此处编写以下代码片段:

http://jsfiddle.net/3TxVx/2/

var story = {
url: "www.nytimes.com/colonizemars.html",
text: "We finally colonized mars!"
};

Handlebars.registerHelper('link', function(object) {
return new Handlebars.SafeString(
"<a href='" + object.url + "'>" + object.text + "</a>"
);
});

var theTemplateScript = $("#header").html();
var theTemplate = Handlebars.compile (theTemplateScript);

var temp = theTemplate(story);
console.log(temp);

$(function() {
$(document.body).append (temp);
});

不确定为什么我在运行时会收到以下错误:

未捕获的类型错误:无法读取未定义的属性“url”

谢谢!

最佳答案

尝试替换

registerHelper('link', function(object)

registerHelper('link', function(text, url)

来自文档:

Handlebars.registerHelper('link', function(text, url) {
text = Handlebars.Utils.escapeExpression(text);
url = Handlebars.Utils.escapeExpression(url);

var result = '<a href="' + url + '">' + text + '</a>';

return new Handlebars.SafeString(result);
});

http://handlebarsjs.com/

关于javascript - HandlebarsJS 未捕获类型错误 : Cannot read property 'url' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20974532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com