最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Multiple path specifications in RequireJS - Stack Overflow

programmeradmin0浏览0评论

I want to be able to inject extra paths, in a file different from the one which contains the config. Can this be done? A bonus question is whether I can directly access "config" variables.

I want to be able to inject extra paths, in a file different from the one which contains the config. Can this be done? A bonus question is whether I can directly access "config" variables.

Share Improve this question asked Sep 30, 2013 at 8:42 sabofsabof 8,1924 gold badges31 silver badges53 bronze badges 1
  • 2 I understand you ask if it possible to add additional paths after require.config() has been run? – z-- Commented Sep 30, 2013 at 8:57
Add a comment  | 

1 Answer 1

Reset to default 18

There is no problem with calling require.config multiple times or from multiple places. You don't have to provide an entire set of configuration on subsequent calls. The new path mappings will be merged with existing ones.

For example, if you did this originally:

require.config({
    paths: {
        foomodule: 'libs/foo',
        jquery:  'libs/jquery'
    }
});

You could later do this to provide a different set of paths for jquery and/or to inject paths for a whole new module not present in the original config:

require.config({
    paths: {
        jquery:  [ 'http://code.jquery.com/jquery-2.0.2', 'http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.2' ],
        // note that foomodule not provided here but still keeps its original configuration
        someothermodule: 'some/other/path'
    }
});

Note, however, that if a module was already loaded based on the original config and you wanted to force it to reload from the new config you might have to call require.undef

Regarding the 2nd part of your question (reading the existing config information), I asked a question on this too and so far have not found a way to do it.

发布评论

评论列表(0)

  1. 暂无评论