[英]Disable component if environment angular is production
我有這樣的事情
環境.ts
export const environment = {
production: false
};
在app.module.ts
我有這樣的事情
@NgModule({
declarations: [AppComponent, VersionComponent],
imports: [],
providers: [],
bootstrap: [AppComponent],
})
export class AppModule { }
我的問題是基於 environment.production 我需要聲明或不聲明VersionComponent
像這樣的東西
if (environment.production) {VersionComponent}
問題是,如果不允許聲明,有人有類似的問題嗎?
模塊元數據由 angular 編譯器使用,它只支持JavaScript 的子集。 幸運的是,該子集包括條件和擴展運算符,允許我們編寫:
declarations: [
AppComponent,
...(environment.production ? [VersionComponent] : []),
],
或者當然,如果任何模板使用未定義的組件,這可能會導致語法錯誤......
或者,您可以使用具有相同選擇器但不執行任何操作的不同定義替換組件:
declarations: [
AppComponent,
environment.production ? VersionComponent : VersionComponentStub,
],
const declarations = [AppComponent];
let extraDeclarations = [];
if(environment.production) {
extraDeclarations = [VersionComponent]
}
const finalDeclarations = declarations.concat(extraDeclarations);
@NgModule({
declarations: finalDeclarations
const _declarations = [AppComponent];
if( environment.production )
_declarations.push( VersionComponent );
@NgModule({
declarations: [
_declarations
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
為我工作;)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.