2015-09-16 84 views
1

我剛剛開始使用Handlebars.js,我已經對HTML中應包含的內容感到困惑。Handlebars 4.0.2應包含哪些源文件?

我設置了一個最小的helloworl HTML文件看起來像:

<html> 
<body> 
    <script id="entry-template" type="text/x-handlebars-template"> 
    <div class="entry"> 
     <h1>{{title}}</h1> 
     <div class="body"> 
     {{body}} 
     </div> 
    </div> 
    </script> 

    <script src="bower_components/jquery/dist/jquery.js"></script> 
    <script src="bower_components/handlebars/handlebars.js"></script> 
    <script src="bower_components/handlebars/handlebars.runtime.js"></script> 
    <script type="text/javascript"> 
    var source = $("#entry-template").html(); 
    var template = Handlebars.compile(source); 
    var context = {title: "My New Post", body: "This is my first post!"}; 
    var html = template(context); 
    document.write(html); 
    </script> 
</body> 
</html> 

,我得到以下錯誤:

[Error] TypeError: undefined is not a function (evaluating 'Handlebars.compile(source)') 
global code (index.html, line 17) 

應包括爲這個小例子,工作還有什麼其他的依賴?使用

版本:

  • 的jQuery 2.1.4
  • 車把4.0.2

回答

0
  1. 您對bower_components/handlebars/handlebars.runtime.js呼叫覆蓋什麼bower_components/handlebars/handlebars.js設置。

  2. 使用handlebars.runtime.js意味着您的模板已經編譯,因此您獲得的Handlebars對象不包含compile函數。

刪除<script src="bower_components/handlebars/handlebars.runtime.js"></script>,你應該是好去:http://jsfiddle.net/nikoshr/dr3gwh7u/

<script src="bower_components/jquery/dist/jquery.js"></script> 
<script src="bower_components/handlebars/handlebars.js"></script> 

<script type="text/javascript"> 
    var source = $("#entry-template").html(); 
    var template = Handlebars.compile(source); 
    var context = {title: "My New Post", body: "This is my first post!"}; 
    var html = template(context); 
    $('body').append(html) 
</script> 
+0

哇。這其實很簡單。車把的安裝說明超級不清楚:「handlebars.js和handlebars.runtime.js是主要的源文件,但該組件中還存在許多其他選項。」(所以我認爲兩者都是必需的) –

相關問題