简体   繁体   English

requirejs项目中第一个define()和require()函数的格式是什么

[英]What will be format of first define() and require() function in requirejs project

Question#1: After requirejs configuration, what will be the format of first require() call to load my main.js 问题#1:配置requirejs后,第一次require()调用的格式将如何加载我的main.js?

App.js App.js

requirejs.config({
    baseUrl: 'lib',
    paths: {
        app: '../app'
    }
});
require(['app/main'], function(m){
    console.log('loading m...');
});

Question#2: And what will be format of first define() function in main.js file. 问题2:main.js文件中第一个define()函数的格式是什么。

Main.js Main.js

define(function () {
    var messages = require('./messages');
    var print = require('print');
});

The only issue I see in the code you show is that your main.js should have its define call like this: 我在显示的代码中看到的唯一问题是main.js应该具有如下的define调用:

define(function (require) {
  var messages = require('./messages');
  var print = require('print');
});

If you pass a function as the first parameter to define , RequireJS will call it with the special modules require , exports and module but at a minimum you must declare the first parameter so that the calls you made inside your factory function are to the local require (passed to the function) rather than the global require . 如果将函数作为要define的第一个参数传递,RequireJS将使用特殊的模块requireexportsmodule调用它,但是至少必须声明第一个参数,以便在工厂函数内进行的调用是对本地 require的调用(传递给函数)而不是全局 require There are a number of issues if you call the global require . 如果您将其称为全局require则会遇到很多问题。 One of them is that relative module references like ./messages won't be resolved correctly. 其中之一是相对模块引用(如./messages将无法正确解析。

The require call you have in your app.js looks fine. 您在app.jsrequire调用看起来不错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM