Параметризованный импорт в машинописи angular - PullRequest
0 голосов
/ 06 апреля 2020

Доступен ли параметризованный импорт в машинописи? Я пытаюсь сделать следующее:

  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 / машинопись, я могу добиться этого?

1 Ответ

0 голосов
/ 06 апреля 2020

Не совсем то, что вы ищете, но у меня есть предложение по другому подходу.

Я думаю, вы можете создать блок *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;
 }

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...