我在 Closure 中使用 angular,但在使用 @export 注释时遇到了问题。特别是能够导出属性定义。我以为我可以使用以下代码进行操作,但它似乎非常喜怒无常。
/**
* @export
* @constructor
*/
com.MyController = function() {
this.greeting = 'hello';
this.goodbye = 'bye'
};
/**
* @export
*/
com.MyController.prototype = {
'sayGoodbye': function() {
return this.goodbye;
},
'sayHello': function() {
return this.greeting;
}
};
有时编译器会导出原型(prototype)上的所有方法,有时它只会导出'sayGoodbye',有时它会完全折叠原型(prototype)上定义的整个对象。
我知道我可以这样做:
/**
* @export
*/
com.MyController.prototype.sayHello = function() {
return this.greeting;
};
哪个有效,但是一直输入它开始变得非常费力,而且可读性较差。
我知道编译器有一个选项来 setExportLocalPropertyDefinitions,但是代码的开源版本目前没有设置这个标志 - 我一直在弄乱 Java 源代码来尝试设置它,但没有运气所以远的。
更新:
因此,如果在不相关的类中存在另一个同名的方法,则原型(prototype)上的方法(例如 sayGoodbye)将被导出。我想它被导出不是因为它像我最初希望的那样定义为一个字符串,而是因为这两种不相关的方法之间存在一些混淆?
最佳答案
确保 export
的方法签名定义为:
Code that uses the @export annotation must either
include closure/base.js, or,
define both goog.exportSymbol and goog.exportProperty with the same method signature in their own codebase.
引用文献
关于configuration - 在高级模式下编译时对象被删除或部分折叠 - Google Closure Compiler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27147909/