代碼打擊不起作用,錯誤味精是:requirejs - 爲什麼加載模塊定義()的作品,但在需要()不
Uncaught Error: Module name "text!templates/bookTemplate.html_unnormalized2" has not been loaded yet for context: _. Use require([])
define(['backbone', 'underscore', 'jquery'], function (Backbone, _, $) {
var bt = require('text!templates/bookTemplate.html');
var BookView = Backbone.View.extend({
initialize: function() {
this.render();
},
render: function() {
var template = _.template(bt, {
name: 'secret book'
});
this.$el.html(template);
}
});
return BookView;
});
接我一招了「 text!templates/bookTemplate.html「來定義(),它的工作原理!以下是工作代碼:
define(['backbone', 'underscore', 'jquery',
'text!templates/bookTemplate.html'], function (Backbone, _, $, bt) {
// var bt = require('text!templates/bookTemplate.html');
var BookView = Backbone.View.extend({
initialize: function() {
this.render();
},
render: function() {
var template = _.template(bt, {
name: 'secret book'
});
console.info('template', template);
this.$el.html(template);
}
});
return BookView;
}); // it is working
正如我的理解,require()和define()在加載模塊中是相同的。它是否正確?你能幫我解釋爲什麼它在定義中工作,而不是在require()中?
我看.. commonJS包裝工程,也define(),但我不能將它們組合在一起。謝謝你,路易斯! –