【问题标题】:Angular Aot Error - How to replace function with a reference to an exported functionAngular Aot 错误 - 如何用对导出函数的引用替换函数
【发布时间】:2017-11-12 14:02:28
【问题描述】:

我有这个代码

export function LocalStorage(key?: string) {
    return WebStorage(localStorage, key);
}

export function WebStorage(webStorage: Storage, key: string) {
        return (target: Object, propertyName: string): void => {
            key = key || propertyName;

            let storageKey = WebStorageUtility.generateStorageKey(key);
            let storedValue = WebStorageUtility.get(webStorage, key);
            ...
            Object.defineProperty(target, propertyName, {
                get: function() { ... },
                set: function(value: any) { .. },
            });
        }
    };

angular cli 上使用 --aot 标志构建项目时,如何解决以下错误。

错误中的错误:静态解析符号值时遇到错误。 不支持函数调用。考虑更换功能或 lambda 引用导出的函数(位置 15:12 在 原始 .ts 文件),在上面解析符号 WebStorage 文件

注意:位置 15:12 在上述代码中为(target: Object, propertyName: string): void => {

在角度组件中,LocalStorage 用作属性装饰器,例如

@LocalStorage() username: string = "";

【问题讨论】:

  • 你如何使用WebStorage,因为这看起来是一个非常普通的装饰器,它应该可以工作。所以我只能指望你在某个地方用错了,也许你忘记了括号? @WebStorage()
  • 是的,这是一个装饰器,但被其他装饰器 LocalStorage 和 SesaionStorage 包裹。我会发布完整的代码并询问。正确地,当我将内部定义为单独的导出函数时,它不起作用,因为它使用了来自外部范围的几个变量。
  • @abdul-wahab,你解决了这个问题吗?可以使用发布您的解决方案吗?
  • 没有还没有解决,我还在用--aot false建设。

标签: angular typescript angular-cli angular2-aot


【解决方案1】:

不确定你如何使用这个WebStorage,它看起来像是一个奇怪的地方出错,但通常人们会按照错误消息的建议执行并提取函数。

因此您可以重构 WebStorage 函数并将内容提取为新的导出方法:

export function WebStorageInner(target: Object, propertyName: string) {
        ...
        Object.defineProperty(target, propertyName, {
            get: function() { ... },
            set: function(value: any) { .. },
        });
    }

以及在最初的使用它:

export function WebStorage(webStorage: Storage, key: string) {
    ...
    return WebStorageInner(target, propertyName);
};

【讨论】:

  • 请看LocalStorage是如何使用的,内部函数如何使用外部函数作用域的key和webstorage。
  • @abdul-wahab,由于Object.defineProperty(,这将产生Function calls are not supported in decorators错误。
猜你喜欢
  • 2017-12-01
  • 1970-01-01
  • 2017-08-27
  • 1970-01-01
  • 2017-06-10
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
  • 2017-03-30
相关资源
最近更新 更多