问题
I have a main module in RequireJS:
require([
'jquery',
'jquery.validate',
'jquery.validate.unobtrusive'
], function ($) {
$(document).ready(function () {
var validator = $("form").validate();
if ($("#txtFirstName").val() !== "")
validator.element("#txtFirstName");
});
});
When I load this page, I get a JavaScript error:
TypeError: $(...).validate is not a function var validator = $("form").validate();**
I don't now why? All scripts are loaded:
回答1:
You'll need to configure shim
to "wire" the dependencies correctly:
require.config({
paths: {
'jquery': 'path-to-jquery',
'jquery.validate': 'path-to-jquery-validate',
'jquery.validate.unobtrusive': 'path-to-jquery-validate-unobtrusive'
},
shim: {
'jquery.validate': ['jquery'],
'jquery.validate.unobtrusive': ['jquery', 'jquery.validate']
}
});
require(['jquery', 'jquery.validate', 'jquery.validate.unobtrusive'], function ($) {
// your code
});
More details (and examples) in the official documentation (look for the "For "modules" that are just jQuery or Backbone plugins..." section).
来源:https://stackoverflow.com/questions/18955018/jquery-validate-not-loaded-requirejs