將基於角度的應用程序(angular-cli)從基於JIT的完好的szenario轉換爲基於AOT的szenario。 AOT構建運行良好。如何在Angular AOT中使用enum(在單獨的文件中)?
在打開生成的網頁時我收到無法讀取屬性'none'的未定義的錯誤。這些錯誤源於未定義的枚舉在運行時。
非常喜歡簡化示例(設有2名獨立的文件)
動作type.enum.ts(在單獨的文件中的枚舉)
export enum eActionType {
none,
actionA
...
}
test.component.ts(組件在單獨的文件中)
import { eActionType } from './action-type.enum';
@Component({ ... })
export class Test {
// @Runtime eActionType (eActionType.none) will be undefined
private/protected/public actionType: eActionType = eActionType.none;
}
eAction雖然被導入,但類型不可用於test.component.ts。
可能的解決方法1 - 同一個文件(不可取)
test.component.ts
// Exported enum within same file
export enum eActionType {
none,
actionA
...
}
@Component({ ... })
export class Test {
// @Runtime eActionType is available
private/protected/public actionType: eActionType = eActionType.none;
}
如果枚舉在同一文件中聲明中枚舉,它將正常工作在運行時。但這不可取,因爲與可導入多個位置的外部文件中的枚舉相比,可維護性會變差。
可能的解決方法2 - 類的靜態屬性代替枚舉(不期望的任一)
動作type.enum.ts(在單獨的文件中的枚舉)
export class eActionType {
static none = 0;
static actionA = 1;
...
}
測試。 component.ts(單獨文件中的組件)
import { eActionType } from './action-type.enum';
@Component({ ... })
export class Test {
// @Runtime eActionType is available as static property of eActionType
private/protected/public actionType: eActionType = eActionType.none;
}
也沒有真正漂亮的解決方案,但至少只有前枚舉的聲明,但不是消費者代碼必須在所有事件中進行更改。
如何強制在外部文件中聲明的適當的枚舉可以在運行時在其他位置訪問?
這將使枚舉可見/可用模板中,而在AOT(whithin JIT私人/保護,而不是公衆是好的) - 如果eActionType會可用。但事實並非如此。 – dsla