1. For details on the framework for building require, see the official website of RequireJS
2. Introduce in the files we write
<script src="assets/js/require.js" data-main="assets/js/main" defer async="true" type="text/javascript"></script>
3. Write the code we want to introduce the external file js
define( function(){.
......... }
or
$(function{
.......... })
) ;
example :
define( $(function () {
$('#doc-prompt-toggle').on('click', function () {
$('#my-prompt').modal({
relatedTarget: this,
onConfirm: function (e) {
alert('What you entered is: ' + e.data || '');
},
onCancel: function (e) {
alert('I don't want to say!');
}
});
});
}) );
4. Write the file code we need to import in main.js
require.config({
paths: {
"testjs" : "testjs"
//Note that testjs needs to be in the same directory as main.js and require.js. If it is not in the same directory, see the official website for details
}
});
require(["testjs"], function ($) {
alert("Test js ");
});