以打字稿成角度的

时间:2020-04-06 06:20:17

标签: typescript

类型化脚本中是否可以使用参数化的导入?我正在尝试执行以下操作:

  1. 在我的环境文件中,我已经指定了程序。
  2. 我想基于如下所示的活动程序,在我的angular模块中导入文件/组件:
/* 
The idea here is that I have a component in two different folders example:

 - /root/components/page-not-found/bingo/page-not-found-component.ts
 - /root/components/page-not-found/pepsi/page-not-found-component.ts
In my environment file I can specify the program as 'bingo' or 'pepsi' which are the folder names and I want to load the component either from 'bingo' directory or from the 'pepsi' directory.
*/

import { environment } from '../environments/environment';
import { PageNotFoundComponent } `./root/components/page-not-found/${environment.program}/page-not-found-component`; // here environment.program refers to the folder name.

@NgModule({
  declarations: [
    PageNotFoundComponent
  ]
});

我尝试了以下操作,但没有成功:

@NgModule({
  declarations: [
    PageNotFoundComponent: (() => import(`./root/components/page-not-found/${environment.program}/page-not-found-component`).then((m) => m.PageNotFoundComponent))()
  ]
});

使用javascript / typescript可以实现吗?

1 个答案:

答案 0 :(得分:0)

并不是您要找的东西,但是我建议采用其他方法。

我认为您可以在组件内部创建一个*if块,并根据那里的环境加载所需的PageNotFoundComopnent

PageNotFoundComoponent html

<page-not-found-component-dev *ngIf="mode==='dev'"></page-not-found-component-dev>
<page-not-found-component-prod *ngIf="mode==='prod'"></page-not-found-component-dev>

PageNotFoundComoponent ts

import { environment } from '../environments/environment';
import { PageNotFoundComponentDev } `./root/components/page-not-found-dev/page-not-found-component-dev`;
import { PageNotFoundComponentProd } `./root/components/page-not-found-prod/page-not-found-component-prod`;

@Component({
...
})
export class PageNotFoundComoponent {

 mode = 'dev';
 constructor(){
  mode = environment.mode;
 }

}