- RequireJS 教程
- RequireJS - 主页
- RequireJS - 概述
- RequireJS - 环境设置
- RequireJS - 配置
- RequireJS - AMD 模块
- RequireJS - 函数定义
- RequireJS - 优化器
- RequireJS - jQuery
- RequireJS - NodeJS
- RequireJS - Dojo
- RequireJS - CommonJS
- RequireJS - 插件
- RequireJS 有用的资源
- RequireJS - 快速指南
- RequireJS - 有用资源
- RequireJS - 讨论
RequireJS - jQuery 使用垫片配置
jQuery 使用垫片配置来定义 jQuery 插件的依赖项,并通过声明依赖项来设置模块值。
加载 jQuery
require(['jquery','jquery.myjsfile1','jquery.myjsfile2'], function($) {
$(function() {
//code here
});
});
示例
以下示例使用 shim 配置来定义 jQuery 插件的依赖项。创建一个名为 index.html 的 html 文件,并在其中放置以下代码 −
<!DOCTYPE html>
<html>
<head>
<title>jQuery Shim Config</title>
<script data-main = "app" src = "lib/require.js"></script>
</head>
<body>
<h2>jQuery Shim Config</h2>
<p>Welcome to Tutorialspoint!!!</p>
</body>
</html>
创建一个名为 app.js 的js 文件,并在其中添加以下代码 −
//You can configure loading modules from the lib directory
requirejs.config ({
"baseUrl": "lib",
"paths": {
"app": "../app"
},
"shim": {
"jquery.shim1": ["jquery"],
"jquery.shim2": ["jquery"]
}
});
//To start the application, load the main module from app folder
requirejs(["app/main"]);
创建一个名为 app 的文件夹,并从此文件夹中加载 main.js 模块 −
define(["jquery", "jquery.shim1", "jquery.shim2"], function($) {
//loading the jquery.shim1.js and jquery.shim2.js plugins
$(function() {
$('body').shim1().shim2();
});
});
再创建一个名为 lib 的文件夹,以存储 require.js 文件和其他 js 文件,如下所示 −
lib/jquery.shim1.js
$.fn.shim1 = function() {
return this.append('<p>This is shim1 config...!</p>');
};
lib/jquery.shim2.js
$.fn.shim2 = function() {
return this.append('<p>This is shim2 config...!</p>');
};
输出
在浏览器中打开 HTML 文件;您将收到以下输出 −
requirejs_jquery.htm
广告