简体   繁体   English

在谷歌闭包编译器中使用模块

[英]Using modules in google closure compiler

I'm just trying to get a basic module working: 我只是想让一个基本模块工作:

testA.js: testA.js:

goog.module('foo');

exports.bar = function(){
  console.log('here');
};

testB.js: testB.js:

var bar = goog.require('foo.bar');
bar();

And I'm running: 我正在跑步:

java -jar ~/Downloads/compiler.jar --js testA.js testB.js

which yields: 产量:

testB.js:1: ERROR - Closure dependency methods(goog.provide, goog.require, etc) must be called at file scope.
var bar = goog.require('foo.bar');
          ^

1 error(s), 0 warning(s)

Any advice? 有什么建议?

Answering this myself. 自己回答这个问题。 So you can't require modules normally from a "normal" closure compiled .js file, only from a module file, which seems to be defined as any file that has goog.module in it. 因此,您不能通常从“普通”闭包编译的.js文件中获取模块,只能从模块文件中获取,模块文件似乎被定义为其中包含goog.module任何文件。

So If in a module, goog.require('module') works fine. 所以如果在模块中, goog.require('module')工作正常。 If not in a module, goog.module.get('module') is needed 如果不在模块中,则需要goog.module.get('module')

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

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