php require_js,require.js的模块化开发

这次给大家带来require.js的模块化开发,require.js模块化开发的注意事项有哪些,下面就是实战案例,一起来看一下。

一、Require.js及AMD

Require.js:是一个非常小巧的JavaScript模块载入框架,是AMD规范最好的实现者之一。

AMD(Asynchronous Module Definition):异步模块定义规范(AMD)制定了定义模块的规则,这样模块和模块的依赖可以被异步加载。这和浏览器的异步加载模块的环境刚好适应(浏览器同步加载模块会导致性能、可用性、调试和跨域访问等问题)。// AMD规范// 第一个参数,id,是个字符串。它指的是定义中模块的名字,这个参数是可选的。如果没有提供该参数,模块的名字应该默认为模块加载器请求的指定脚本的名字。如果提供了该参数,模块名必须是“顶级”的和绝对的(不允许相对名字)。// 第二个参数,dependencies,是个定义中模块所依赖模块的数组。依赖模块必须根据模块的工厂方法优先级执行,并且执行的结果应该按照依赖数组中的位置顺序以参数的形式传入(定义中模块的)工厂方法中。// 第三个参数,factory,为模块初始化要执行的函数或对象。如果为函数,它应该只被执行一次。如果是对象,此对象应该为模块的输出值。define(id?, dependencies?, factory);

二、Require.js使用

举例中用到了个中文转换拼音的插件(GitHub地址:https://github.com/sxei/pinyinjs/),这个插件挺好用的,但由于中文中存在多音字,经常会出现不符合常规的拼音,比如(贾宝玉 => "gu bao yu")├─scripts

│─plus

├─pinyin

│ ├─dist

│ │ ├─pinyin_dict_firstletter.js

│ │ ├─pinyin_dict_notone.js

│ │ ├─pinyin_dict_polyphone.js

│ │ └─pinyin_dict_withtone.js

│ └─pinyinUtil.js

├─jquery.js

├─plusMain.js

├─require.js

└─test.js

2、引入require.js

3、挂载模块

[1] 局部加载//test.jsdefine(function () { return {

add: function (a, b) {

alert("传参没用上,哈哈");

}

}

});//页面代码$(function () {

require(["/scripts/plus/test.js"], function (h) {

h.add(1, 2);

);

})

[2] 全局加载//plusMain.jsrequire.config({

paths: { jquery: "jquery",

}

});//页面引用

}

});

还可引用远端文件,也可以同时引用本地文件,当远端文件加载失败时就会加载本地文件了。require.config({

paths: { jquery: ["http://libs.baidu.com/jquery/2.0.3/jquery", "jquery"],

}

});

通过require加载的模块一般都需要符合AMD规范即使用define来申明模块,但是部分时候需要加载非AMD规范的js,这时候就需要用到另一个功能:shim。

shim解释起来也比较难理解,shim直接翻译为"垫",其实也是有这层意思的,目前我主要用在两个地方:

一个是,配置模块的依赖关系:例如,拼音插件pinyinUtil.js需要依赖字典才可以正常转换。1 require.config({ 2 paths: { 3 jquery: "jquery", 4 pinyin_dict_firstletter: "pinyin/dict/pinyin_dict_firstletter", 5 pinyin_dict_withtone: "pinyin/dict/pinyin_dict_withtone", 6 pinyin: "pinyin/pinyinUtil" 7 }, 8 shim: { 9 pinyin: {10 deps: ["pinyin_dict_firstletter", "pinyin_dict_withtone"],// 依赖模块集合11 }12 }13 });14 15 //可以简写成16 shim: {17 pinyin: ["pinyin_dict_firstletter", "pinyin_dict_withtone"]18 }

也可以依赖css文件require.config({

paths: {

select2: "select2/select2.min"

},

shim: {

select2: ["/scripts/plus/select2/select2.min.css"]

},

});

另一个是,加载非AMD模块输出,将非标准的AMD模块"垫"成可用的模块。// test.js(function () { var NGY = {

Hi: function() {

alert("跨越过去之后");

}

}

window.NGY = NGY;

})();//配置require.config({

paths: {

ngy: "test"

},

shim: {

ngy: { exports: "NGY" }

},

});//使用,不需要声明变量就可以直接使用require(["ngy"], function () {

NGY.Hi();

});

4、使用requirerequire(["jquery", "pinyin"], function ($, pinyin) {

$("span").html("谁最可爱?"); var dl = pinyin.getPinyin('当然是我');

});

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

以上就是require.js的模块化开发的详细内容,更多请关注php中文网其它相关文章!

article_wechat2021.jpg?1111

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
index.js:4130 Cannot find module './uni_modules/uview-ui/components/u-navbar/u-navbar.vue' Qld0 @ index.js:4130 __webpack_require__ @ index.js:854 fn @ index.js:151 JLrY @ index.js:1430 __webpack_require__ @ index.js:854 fn @ index.js:151 eval @ kyBj:2 kyBj @ index.js:4897 __webpack_require__ @ index.js:854 fn @ index.js:151 eval @ main.js:36 Tglg @ index.js:4322 __webpack_require__ @ index.js:854 fn @ index.js:151 1 @ index.js:1057 __webpack_require__ @ index.js:854 checkDeferredModules @ index.js:46 (anonymous) @ index.js:994 (anonymous) @ index.js:997 index.js:4131 1. 排查组件名称拼写是否正确 Qld0 @ index.js:4131 __webpack_require__ @ index.js:854 fn @ index.js:151 JLrY @ index.js:1430 __webpack_require__ @ index.js:854 fn @ index.js:151 eval @ kyBj:2 kyBj @ index.js:4897 __webpack_require__ @ index.js:854 fn @ index.js:151 eval @ main.js:36 Tglg @ index.js:4322 __webpack_require__ @ index.js:854 fn @ index.js:151 1 @ index.js:1057 __webpack_require__ @ index.js:854 checkDeferredModules @ index.js:46 (anonymous) @ index.js:994 (anonymous) @ index.js:997 index.js:4132 2. 排查组件是否符合 easycom 规范,文档:https://uniapp.dcloud.net.cn/collocation/pages?id=easycom Qld0 @ index.js:4132 __webpack_require__ @ index.js:854 fn @ index.js:151 JLrY @ index.js:1430 __webpack_require__ @ index.js:854 fn @ index.js:151 eval @ kyBj:2 kyBj @ index.js:4897 __webpack_require__ @ index.js:854 fn @ index.js:151 eval @ main.js:36 Tglg @ index.js:4322 __webpack_require__ @ index.js:854 fn @ index.js:151 1 @ index.js:1057 __webpack_require__ @ index.js:854 checkDeferredModules @ index.js:46 (anonymous) @ index.js:994 (anonymous) @ index.js:997 index.js:4135 3. 若组件不符合 easycom 规范,需手动引入,并在 components 中注册该组件 Qld0 @ index.js:4135 __webpack_require__ @ index.js:854 fn @ index.js:151 JLrY @ index.js:1430 __webpack_require__ @ index.js:854 fn @ index.js:151 eval @ kyBj:2 kyBj @ index.js:4897 __webpack_require__ @ index.js:854 fn @ index.js:151 eval @ main.js:36 Tglg @ index.js:4322 __webpack_require__ @ index.js:854 fn @ index.js:151 1 @ index.js:1057 __webpack_require__ @ index.js:854 checkDeferredModules @ index.js:46 (anonymous) @ index.js:994 (anonymous) @ index.js:997 Request.js:53 Uncaught TypeError: f is not a function at Request.setConfig (Request.js:53:23) at Object.install (http.interceptor.js:4:24) at Vue.use (chunk-vendors.js:7461:22) at eval (main.js:160:5) at Object.Tglg (index.js:4322:1) at __webpack_require__ (index.js:854:30) at fn (index.js:151:20) at 1 (index.js:1057:18) at __webpack_require__ (index.js:854:30) at checkDeferredModules (index.js:46:23)解决
07-14

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值