我想在加载 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');
})
});
我收到以下错误
未捕获错误:不匹配的匿名define()模块
请帮助我提前致谢。
最佳答案
该错误是因为您有:
<script src="/static/js/LoadView.js"></script>
docs说:
If you manually code a script tag in HTML to load a script with an anonymous define() call, this error can occur.
这就是你正在做的事情。
在解决方案中:
Be sure to load all scripts that call define() via the RequireJS API. Do not manually code script tags in HTML to load scripts that have define() calls in them.
If you manually code an HTML script tag, be sure it only includes named modules, and that an anonymous module that will have the same name as one of the modules in that file is not loaded.
因此,只需使用主文件中的 requireJS 加载它或命名模块即可
关于javascript - 第一次加载主干 View 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42976989/