2017-03-23 90 views
0

我想在我的html頁面加載時自動加載我的javascript中的一個。 加載我想加載我的js文件的html頁面。問題首次加載骨幹視圖

<!DOCTYPE html> 
<html> 

<head> 
    <script src="/static/js/LoginApp.js"></script> 
    <meta charset="ISO-8859-1"> 
    <title>Insert title here</title> 
</head> 

<body> 
    <div class="container"> 
     <h1>Backbone Tutorial Blogroll App</h1> 
     <button type="button" id="newButton">Click Me!</button> 
     <script src="/static/js/require.js"></script> 
     <script src="/static/js/LoadView.js"></script> 
    </div> 
</body> 

</html> 

我想加載JavaScript是

LoadView.js

define([ 
     'jquery', 
     'underscore', 
     'backbone', 
     'LoadView', 
     'text!NewViewCheck.html', 

     ], function($, _, Backbone, LoadView, NewViewCheck) { 
    var Task = Backbone.Model.extend({ 
     defaults: {} 
    }); 
    TheView = Backbone.View.extend({ 
     model: new Task(), 
     events: { 
      'click #newButton': 'initializeView', 
     }, 
     //'template' provides access to instance data when rendering the view 
     template: _.template(NewViewCheck), 
     initialize: function() { 
      console.log('Inside the initialize function'); 
      this.render(); 
     }, 
     render: function() { 
      //this.$el.html(this.template()); 
      console.log(labelsLocale); 
      this.$el.html(this.template({})); 
      $('#dialogContent').empty().append(this.$el); 
      $('#addUserDefinedOption').modal('show'); 
     }, 
     initializeView: function() { 
      var theView = new TheView(); 
      console.log('abc'); 
     }, 
    }); 
    $(document).ready(function() { 
     //console.log('abc'); 
    }) 
}); 

我收到以下錯誤 Uncaught Error: Mismatched anonymous define() module

請幫我在此先感謝。

+0

是您的''的文字!NewViewCheck.html''模板駐留在同一路徑下的定義? –

+0

您可以告訴我們您目錄中的NetViewCheck.html'的物理路徑嗎? –

+0

它存在於我的spring應用程序的公共文件夾中。 –

回答

1

的錯誤是因爲你有:

<script src="/static/js/LoadView.js"></script> 

docs說:

如果你手工編碼的HTML腳本標記加載腳本使用匿名定義()調用,這錯誤可能發生。

這就是你在做什麼。

而在解決方案:

  • 一定要加載調用定義()通過RequireJS API的所有腳本。不要在HTML中手動編寫腳本標記以加載其中定義了()調用的腳本。

  • 如果您手動編寫HTML腳本標記,請確保它只包含命名模塊,並且不會加載與該文件中的某個模塊具有相同名稱的匿名模塊。

單純用requireJS它加載從您的主文件或命名模塊