简体   繁体   English

车把没有定义

[英]Handlebars is not defined

I am new at JS and never tried Handlebars before. 我是JS的新手,之前从未尝试过Handlebars。 I'm trying a very bare-bones example from a tutorial. 我正在尝试一个教程中的一个非常简单的例子。 I have downloaded handlebars-v4.0.5.js from the Handlebars website and included it in my page, as follows, along with a template I precompiled, name-table.js . 我从Handlebars网站下载了handlebars-v4.0.5.js ,并将其包含在我的页面中,如下所示,以及我预编译的模板name-table.js I'm trying my god damned hardest to complete this tutorial but I can't, because when I run the following code, I get ReferenceError: Handlebars is not defined which I can't for the life of me understand, if the file I downloaded from Handebars' own site is in any way valid. 我正在努力完成这个教程,但我不能,因为当我运行以下代码时,我得到了ReferenceError: Handlebars is not defined哪个我不能为我的生活理解,如果文件我从Handebars自己的网站下载的内容无论如何都有效。

<html>
    <head>
        <meta charset='UTF-8'>
        <title>Test Page!</title>
        <script type='text/javascript'>
            function init() {
                document.getElementById('button').addEventListener('click', buttonClick, false);
            }
            function buttonClick() {
                var injection = Handlebars.templates['name-table'];
                document.getElementById('button').innerHTML = injection;
                console.log('hello, world.');
            }

            window.addEventListener('load', init, false);
        </script>
    </head>
    <body>
        <button id='button'>Button</button>
        <div id='content'></div>

        <script type='text/javascript' rel='js/handlebars-v4.0.5.js'></script>
        <script type='text/javascript' rel='js/name-table.js'></script>
    </body>
</html>

Edit: The answer I marked solved the problem. 编辑:我标记的答案解决了问题。 Another error appears in this code, and I want to let anyone reading this know that var injection as defined in this code is a function, not a string as I had thought. 此代码中出现了另一个错误 ,我想让任何阅读此内容的人知道此代码中定义的var injection是一个函数,而不是我想到的字符串。 This code will work if rel is changed to src as in the answer given, and if we use document.getElementById('button').innerHTML = injection(); 如果在给出的答案中将rel更改为src ,并且如果我们使用document.getElementById('button').innerHTML = injection(); ,则此代码将起作用document.getElementById('button').innerHTML = injection(); (note the parens). (注意parens)。

You are not loading in Handlebars (or your name-table script). 您没有加载Handlebars(或您的名称表脚本)。 You currently have the following markup: 您目前有以下标记:

    <script type='text/javascript' rel='js/handlebars-v4.0.5.js'></script>
    <script type='text/javascript' rel='js/name-table.js'></script>

You should be using the src attribute instead of the rel attribute for script tags. 您应该使用src属性而不是脚本标记的rel属性。

    <script type='text/javascript' src='js/handlebars-v4.0.5.js'></script>
    <script type='text/javascript' src='js/name-table.js'></script>

The Mozilla documentation does not specify the rel attribute as a valid script tag attribute. Mozilla文档未将rel属性指定为有效的脚本标记属性。

Include this line: 包括这一行:

<script src="https://twitter.github.io/typeahead.js/js/handlebars.js"></script>

You are done. 你完成了。 Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM