2017-04-06 102 views
0

车把我遇到了以下错误:如何使用与RequireJS

Uncaught TypeError: Cannot call method 'compile' of undefined. 

RequireJS配置:

requirejs.config({ 
    baseUrl: "resources", 
    paths: { 
     'app':'lib', 
     'jquery': 'lib/jquery-1.9.1', 
     'bootstrap': 'lib/bootstrap', 
     'html5shiv': 'lib/html5shiv', 
     'spin': 'lib/spin', 
     'respond': 'lib/respond', 
     'underscore': 'lib/underscore', 
     'backbone': 'lib/backbone', 
     'handlebars': 'lib/handlebars-v3.0.3', 
     'template': 'app/templates' 
    }, 
    shim: { 
     html5shiv: { 
      deps: ['jquery'] 
     }, 
     respond: { 
      deps: ['jquery'] 
     }, 
     bootstrap: { 
      deps: ['jquery'] 
     }, 
     jquery: { 
      exports: '$' 
     }, 
     underscore: { 
      exports: '_' 
     }, 
     backbone: { 
      deps: ['jquery', 'underscore'], 
      exports: 'Backbone' 
     }, 
     handlebars: { 
      exports: "Handlebars" 
     } 
    } 
}); 

require([ 
    'jquery', 
    'underscore', 
    'backbone', 
    'handlebars', 
    'app/Router' 
], function($, 
      _, 
      Backbone, 
      Handlebars, 
      Router) { 
    var router = new Router(); 
    Backbone.history.start(); 
}); 

查看:

define([ 
    'backbone', 
    'handlebars', 
    'text!templates/mytemplate.html' 
], function(Backbone, Handlebars, Template){ 

    MyView = Backbone.View.extend({ 
     tagName: 'li', 
     template: Handlebars.compile(Template), 

     render: function() { 
      this.$el.html(this.template(this.model.toJSON())); 
      return this; 
     } 
    }); 

    return MyView; 
}); 

回答

0

shim是不支持的库AMD。您正在使用的句柄版本可能支持AMD,并且未定义名为Handlebars的全局变量。因此你会得到错误。尝试从垫片中删除handlebars配置。