对“模块"的RequireJS调用 [英] RequireJS call to "module"

查看:52
本文介绍了对“模块"的RequireJS调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个继承的旧版应用程序,该应用程序使用RequireJS和django-require加载许多JavaScript模块.我是RequireJS的新手.

我在其中一个模块中有一个define调用

  define([模块","lib/jquery","./scripts/*myScriptName*",[...]],函数(模块,$,myScriptName,[...]){var pr = module.prefix;*与公关做某事*返回myScriptName;} 

模块"是文字的值;任何非文字的内容都以我"开头

我怀疑这里的模块"是指 requirejs.config config 块中的 myScriptName 块(此配置块是同级的到配置中的 paths 块和 deps 块):

  {配置":{"myScriptName":{"prefix":"http://myhost.mydomain.tld/path/to/myScriptName",[...]},},"deps":["path/to/my/dep"],路径":{"lib":路径/到/我/lib","jquery":路径/到/我/lib/jquery"}} 

这是 django-require 的功能(不存在标记)还是requirejs的功能,并且这是某个地方记录的标准块吗?谢谢!

解决方案

RequireJS定义了几个特殊模块.其中之一名为 module .它可以将当前模块作为对象进行访问.例如,您可以通过将 module.exports 设置为一个值来导出某些内容.这只是从模块中导出内容的一种方法.可以通过 module.id 获得模块名称,并通过 module.uri 获得URL(是的,即 uri ).

还有一个函数 module.config(),您可以使用该函数访问传递给的对象中 config 设置的字段的值> require.config().问题中当前的代码显示不正确,应该是:

  var pr = module.config().prefix; 

另一个特殊模块是 require ,它仅提供可用于加载模块的功能的参考.

所有这些都是 API 中记录的RequireJS功能.>

I'm working on a legacy app that I inherited that uses RequireJS and django-require to load many JavaScript modules. I'm somewhat new to RequireJS.

I have a define call in one of the modules

define([
    "module",
    "lib/jquery",
    "./scripts/*myScriptName*",
    [...]
], function(module, $, myScriptName, [...]) {
    var pr = module.prefix;
    *do something with pr*

  return myScriptName;
}

"module" is a value of the literal; anything not literal starts with "my"

I suspect "module" here refers to a myScriptName block in a config block in requirejs.config (this config block is a sibling to the paths block and deps block in the config):

{
    "config": {
        "myScriptName": {
            "prefix": "http://myhost.mydomain.tld/path/to/myScriptName",
            [...]
        },
    },
    "deps": ["path/to/my/dep"],
    "paths": {
        "lib": "path/to/my/lib",
        "jquery": "path/to/my/lib/jquery"
    }
}

Is this a feature of django-require (no tag exists), or of requirejs, and is this a standard block that is documented somewhere? Thanks!

解决方案

RequireJS defines a couple special modules. One of them is named module. It gives access to the current module as an object. For instance, you can export something by setting module.exports to a value. This is just one way to export things from a module. It is possible to get the module name through module.id and its URL through module.uri (yes, that's uri).

There is also a function module.config() that you can use to access the value of the field of the config setting in the object you pass to require.config(). The code currently in the question appears incorrect, it should be:

var pr = module.config().prefix;

The other special module is require, which just gives a reference to the function you can use to load modules.

All of this is stock RequireJS functionality documented in the API.

这篇关于对“模块"的RequireJS调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆