我有一個枚舉,我試圖注入一個角度的應用程序類。我怎樣才能讓我的課程認識到這個枚舉?
我不能讓打字稿承認枚舉的屬性,並得到錯誤Property 'xs' does not exist on type 'appBreakpoints'
代碼:
// appBreakpoints.ts file
export enum appBreakpoints {
xs = 1, // convert xs to truthy value
sm,
md,
lg,
xl,
xxl
}
// In app.module
import { appBreakpoints } from './appBreakpoints'
@NgModule({
providers: [
{ provide: appBreakpoints, useValue: appBreakpoints}
]
});
// In some service
import { appBreakpoints } from './appBreakpoints';
import { Inject } from '@angular/core';
class MyClass {
constructor(@Inject(appBreakpoints) private appBreakpoints: appBreakpoints) {
if (0 < this.appBreakpoints.xs) { // TS ERROR: Property 'xs' does not exist on type 'appBreakpoints'
console.log('something being logged');
}
}
}
我怎樣才能得到打字稿識別枚舉的屬性,所以我可以使用它們在我的班上?
感謝。它讓我的代碼工作。我在app-config文件中有枚舉,只是想讓其他可能需要枚舉的服務可用。 – Jonathan002