requirejs.config()有什么作用?

时间:2015-04-03 19:00:51

标签: javascript requirejs

我无法理解requirejs.config()函数。

requirejs.config({
    paths: {
        'text': '../lib/require/text',
        'durandal':'../lib/durandal/js',
        'plugins' : '../lib/durandal/js/plugins',
        'transitions' : '../lib/durandal/js/transitions',
        'knockout': '../lib/knockout/knockout-3.1.0',
        'bootstrap': '../lib/bootstrap/js/bootstrap',
        'jquery': '../lib/jquery/jquery-1.9.1'
    },
    shim: {
        'bootstrap': {
            deps: ['jquery'],
            exports: 'jQuery'
       }
    }
});

该功能有什么作用?请不要指示我阅读文档,因为我已阅读它并仍然发现它令人困惑。我需要一个关于这个功能的简单解释。

这些脚本是异步加载的吗?

2 个答案:

答案 0 :(得分:1)

它为脚本路径创建别名ant告诉如何在加载时解释bootstrap(非AMD脚本)。什么都没装。你必须要求:

// we load two dependencies here
// `knockout` and `bootstrap` are expanded to values in config
// .js added to values
// callback function called when all dependencies are loaded
require(['knockout', 'bootstap'], function(Knockout, $) {
    // jQuery is passed to this function as a second parameter
    // as defined in shim config exports
});

答案 1 :(得分:0)

路径类似于声明/定义。例如,

jquery: '../bower_components/jquery/dist/jquery',

您可以稍后加载此lib,如下所示。

define([
  'jquery'
], function (jquery) {
   //initialize or do something with jquery
}

您不必指定库的绝对路径。

shim 中,您将定义依赖项。例如,

paths: {
  template: '../templates',
  text:  '../bower_components/requirejs-text/text',
  jquery: '../bower_components/jquery/dist/jquery',
  backbone: '../bower_components/backbone/backbone',
  underscore: '../bower_components/underscore/underscore',
  Router: 'routes/router'
},
shim: {
  'backbone': ['underscore', 'jquery'],
  'App': ['backbone']
}

此处主干依赖于下划线 jquery 。因此,在主干开始加载之前,将加载这两个库。类似地,加载主干后将加载 App

如果您熟悉骨干和快递,您可能会发现这个回购有用。

https://github.com/sohel-rana/backbone-express-boilerplate

相关问题