为什么我不能在require.js中两次使2个路径指向同一个文件

Why can't I make 2 paths point to the same file twice in require.js?

本文关键字:2个 两次 路径 文件 同一个 不能 require js 为什么      更新时间:2023-09-26

我想有 2 个指向同一模块的 requirejs 路径:

var require = {
    paths: {
        "hardPath" : "file",
        "alias" : "file"
    }
}

当我运行我的应用程序时,我收到"别名"的加载超时错误。如果我应用程序的 js 文件只引用"hardPath"或"别名"中的一个,而不引用另一个,它工作正常。但是如果我有引用这两个的 js 文件,我会得到加载超时。有什么理由要求.js不允许这样做吗?

RequireJS 用于此目的的 API 是 map。您可以对其进行配置,以便在任何模块请求"别名"时自动为其提供"hardPath":

require.config({
    // paths, shim, etc.
    // and now remap requests for the wrong module name to the right one
    map: {
        '*': {
            'alias': 'hardPath'
        }
    }
});

从上面链接的文档:

此外,路径配置仅用于为模块 ID 设置根路径,而不用于将一个模块 ID 映射到另一个模块 ID。

相关文章: