2017-04-19 83 views

回答

1

您可以使用require.toUrl("my-dependency")获取到当前模块的网址相对。举例来说,这应该输出相对URL:

define(function (require) { 
    console.log(require.toUrl("my-dependency")); 
}); 

您需要使用本地require这让RequireJS知道什么模块是否正在从调用。 (上面不需要依赖关系列表,如果你没有通过列表,RequireJS使用[“require”,“module”,“exports”]的依赖关系列表。)

我不知道单步的方式得到一个绝对的URL,但如果你需要一个绝对URL,你可以结合通过module.urirequire.toUrl返回值提供的网址:

define(function (require, module) { 
    console.log(join(module.uri, require.toUrl("my-dependency"))); 
}); 

join是一个函数,能够加盟网址。您可以使用URI.js,其他一些库,或者自己写。)