简体   繁体   English

为什么AMD的相对导入无法在requirejs中工作?

[英]Why don't relative imports with AMD work in requirejs?

So, if you have a folder: 因此,如果您有一个文件夹:

- demo/js/foo/foo.js
- demo/js/foo/bar/a.js
- demo/js/foo/bar/b.js

Then defining an AMD module's as: 然后将AMD模块定义为:

foo.js: define(['./bar/a.js', './bar/b.js'], function(a, b) { console.log(a, b); });
a.js: define([], function() { return {a:'a'}; });
b.js: define([], function() { return {b:'b'}; });

Then if you import the module like this: 然后,如果您像这样导入模块:

require(['./foo/foo'], function() { ... }

Gives: 得到:

"NetworkError: 404 Not Found - http://localhost:3005/demo/foo/bar/a.js"
"NetworkError: 404 Not Found - http://localhost:3005/demo/foo/bar/b.js"
Error: Script error for: foo/bar/a.js http://requirejs.org/docs/errors.html#scripterror
Error: Script error for: foo/bar/b.js http://requirejs.org/docs/errors.html#scripterror

Why doesn't this work? 为什么不起作用?

I've read some obscure posts in the requirejs forum saying this is 'working as intended' because 'imports are names, they are not relative paths'. 我在requirejs论坛上读了一些晦涩的帖子,说这是“按预期工作”,因为“导入是名称,它们不是相对路径”。 ...and that you should resolve this issue using the 'map' function. ...并且您应该使用“地图”功能解决此问题。

... ...

Right! 对! Well, I won't go in to how obvious this behavior is, or how vastly unhelpful threads like https://groups.google.com/forum/#!searchin/requirejs/relative/requirejs/Zmh7EV5fR2M/4OM-ss5g3DEJ are; 好吧,我不会讲这种行为有多明显,或者像https://groups.google.com/forum/#!searchin/requirejs/relative/requirejs/Zmh7EV5fR2M/4OM-ss5g3DEJ这样的线程是多么无用的; let's just get to the point. 让我们直接说清楚。

How are you supposed to make this work? 如何应该使这项工作?

Obviously if you import an arbitrary module using bower, and it lives in say, js/lib/obscure.js/dist/obscure.js you need to setup your config: 显然,如果您使用Bower导入任意模块,并且该模块位于js / lib / obscure.js / dist / obscure.js中,则需要设置配置:

require.config({
    paths: {
        obscure: 'lib/obscure.js/dist/obscure'
    }
});

...but this seems to mean that all 'amd importable' modules end up being massive amalgamations of one simple file with all the modules in it. ...但这似乎意味着所有“ amd可导入”模块最终都是其中一个包含所有模块的简单文件的大量合并。

Using baseUrl is not a solution, because, as above, you expect to have multiple isolated 'islands' of javascript which are installed as modules, which 1) need to refer to each other, but 2) also need to be able to refer to their own internal, relative modules. 使用baseUrl并不是解决方案,因为如上所述,您希望将javascript的多个孤立的“岛”作为模块安装,其中1)需要互相引用,但2)还需要能够引用自己的内部,相对模块。

Seems extremely weird. 似乎异常怪异。

Once again, how are you supposed to make this work in the non-trivial case? 再次,如何你应该做的不平凡的情况下,这个工作?

Edit: 编辑:

Surely you say, you're doing it wrong and just not telling us all the things you're doing. 当然,您说的是,您做错了,只是没有告诉我们您正在做的所有事情。 Well, see for yourself. 好吧,自己看看。 An exact copy of this not working is now here on github: https://github.com/shadowmint/requirejs-example github上的错误副本现在在这里: https : //github.com/shadowmint/requirejs-example

Relative imports do work. 相对进口确实有效。

The problem with your code is that you list your module names together with the .js extension. 代码的问题是您列出了模块名称以及.js扩展名。 You should never specify a module name with the .js extension. 您永远不要以.js扩展名指定模块名称。 If you do put the extension, you're essentially telling RequireJS "I already know that the module I want is at the end of the path I'm giving you; don't mess with this path" so your RequireJS configuration won't affect how the path you put in your dependencies is resolved. 如果您确实放置了扩展名,则实际上是在告诉RequireJS“我已经知道我想要的模块在我给您的路径的末尾;不要弄乱这个路径”,因此您的RequireJS配置不会影响放置在依赖项中的路径的解析方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM