Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/421.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript HandlebarsJS未捕获类型错误:无法读取属性';url';未定义的_Javascript_Jquery_Handlebars.js - Fatal编程技术网

Javascript HandlebarsJS未捕获类型错误:无法读取属性';url';未定义的

Javascript HandlebarsJS未捕获类型错误:无法读取属性';url';未定义的,javascript,jquery,handlebars.js,Javascript,Jquery,Handlebars.js,我刚刚开始学习handlebar.js,我使用handlebar.js站点()在这里帮助编写以下代码片段: var故事={ 网址:“www.nytimes.com/coloniemars.html”, 文字:“我们终于征服了火星!” }; 把手.注册表帮助器('link',函数(对象){ 返回新把手。安全字符串( "" ); }); var theTemplateScript=$(“#头”).html(); var theTemplate=handlebar.compile(模板脚本); va

我刚刚开始学习handlebar.js,我使用handlebar.js站点()在这里帮助编写以下代码片段:

var故事={
网址:“www.nytimes.com/coloniemars.html”,
文字:“我们终于征服了火星!”
};
把手.注册表帮助器('link',函数(对象){
返回新把手。安全字符串(
""
);
});
var theTemplateScript=$(“#头”).html();
var theTemplate=handlebar.compile(模板脚本);
var temp=模板(故事);
控制台日志(temp);
$(函数(){
$(document.body).append(temp);
});
不确定运行时为什么会出现以下错误:

未捕获的TypeError:无法读取未定义的属性“url”

谢谢

尝试更换

registerHelper('link', function(object)

从文档中:

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);
});
handlebar.registerHelper('link',函数(文本,url){
text=handlebar.Utils.escapeeexpression(text);
url=handlebar.Utils.escapeeexpression(url);
var结果=“”;
返回新把手。安全字符串(结果);
});

结果是Handlebar对数据或上下文的格式非常挑剔。我对story对象做了以下更改,它成功了

    var data = { 
            story :  {
                url: "www.nytimes.com/colonizemars.html",
                text: "We finally colonized mars!"
            }
    };
现在我的整个代码如下所示:

<script id="header" type="text/x-handlebars-template">
    {{{link story}}}
</script>
<script type="text/javascript">

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

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

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

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

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

</script>

{{{{链接故事}}}
把手.注册表帮助器('link',函数(对象){
返回新把手。安全字符串(“”)
);
});
变量数据={
故事:{
网址:“www.nytimes.com/coloniemars.html”,
文字:“我们终于征服了火星!”
}
};
var theTemplateScript=$(“#头”).html();
var theTemplate=handlebar.compile(模板脚本);
var temp=模板(数据);
控制台日志(temp);
$(函数(){
$(document.body).append(temp);
});
    var data = { 
            story :  {
                url: "www.nytimes.com/colonizemars.html",
                text: "We finally colonized mars!"
            }
    };
<script id="header" type="text/x-handlebars-template">
    {{{link story}}}
</script>
<script type="text/javascript">

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

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

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

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

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

</script>