如何访问Require.js的配置 [英] How can I access configuration of Require.js

查看:148
本文介绍了如何访问Require.js的配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在模块中获取Require.js的 baseUrl 配置,但我找不到它的存储位置。

I try to get the baseUrl configuration of Require.js inside a module, but I can't find where it is stored.

define([], function() {
  // Here I'd like to access the `baseUrl` require.js is using
  var baseUrl = requirejs.config().baseUrl;
});

在我的情况下, baseUrl 已设置使用脚本文件的 data-main 属性来完成Require.js。

In my case, the baseUrl is set up by Require.js using the data-main attribute of the script file.

我知道我可以请求 module 访问 config 属性(例如 define(['module'])),但我找不到如何访问更高级别的配置选项。

I know I can request module to access the config attributes (e.g. define(['module'])), but I can't find how to access the higher level of configuration option.

推荐答案

你想要吗?使用 toUrl

define({
    load: function (name, parentRequire, load, config) {
           var fullUrl = parentRequire.toUrl("foo/bar.css");
    }
});

编辑

从require.js开始2.1.3,致电 toURL 返回没有扩展名的路径。因此,要获得baseUrl:

edit:
Starting in require.js 2.1.3, calling toURL return the path without extension. As so, to get the baseUrl:

var baseURL = require.toUrl();

这篇关于如何访问Require.js的配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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