【问题标题】:How do requirejs paths work?requirejs 路径如何工作?
【发布时间】:2014-03-29 18:45:23
【问题描述】:

我不明白路径是如何工作的

common.js

requirejs.config({baseUrl: '../js/lib',
    paths: {
        jquery: ['//code.jquery.com/jquery-latest.min.js', 'lib/jquery'],
        bootstrap: 'lib/bootstrap.min',
        html5shiv: '//oss.maxcdn.com/libs/html5shiv/3.7.0/htnl5shiv',
        respond: '//oss.maxcdn.com/libs/respond.js/1.3.0/respond.min',
        menu: 'app/smooth-sliding-menu'
    },
    shim: {
        'bootstrap': {deps: ['jquery']}
    }
});

index.js

require(['../common'], function (common) {
    require(['google-map-loader', '//maps.googleapis.com/maps/api/js?key=AIzaSyC6ZePb8iIYGAvbfL8YPflJ-o5LLtdZjGg&sensor=false"'])
});

1) 我在 Google 控制台中没有收到任何错误 2)我不知道发生了什么。 3)我不明白我应该使用哪种类型的路径(相对或绝对)

请给我一些关于我的问题的解释?

【问题讨论】:

  • “requirejs 路径如何工作”过于宽泛。你必须更具体。你显然有问题。你说没有错误信息。 (使用enforceDefine 可能会触发一些您现在没有收到的错误消息。)这很公平,但肯定会发生您不希望发生的事情或您期望发生的事情没有发生。你应该提到它是什么。

标签: javascript requirejs relative-path


【解决方案1】:

路径通常是相对的,但是我们发现在工作中维护起来太复杂了,我们将路径设置为绝对路径。由于我们在服务器端使用MVC/C#,因此只需在_Layout.cshtml 中包含requireJS 配置数据就很简单了。

你也可以对你的配置做同样的事情。

如果你是严格的 Javascript,找出你的路径有点棘手,但一旦你这样做了,原理是一样的。

服务器端代码(为清楚起见进行了修剪):

window.requreJsConfig = {
    paths: {
        "data": appRoot + "/Scripts/SharedData",
        "resources": appRoot + "/Scripts/resources"
    },
    shim: {
        bootstrap: {
            deps: ['jquery'],
            exports: "bootstrap"
        }
    }
};

【讨论】:

    猜你喜欢
    • 2015-02-04
    • 1970-01-01
    • 2013-05-25
    • 2013-01-10
    • 1970-01-01
    • 2010-09-15
    • 2015-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多