简体   繁体   中英

Using modules in google closure compiler

I'm just trying to get a basic module working:

testA.js:

goog.module('foo');

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

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.

So If in a module, goog.require('module') works fine. If not in a module, goog.module.get('module') is needed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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