[英]Use a service inside a decorator in Angular 4
I have a service as a configuration file where I store all the components that I want to use in my application. 我有一个服务作为配置文件,我存储了我想在我的应用程序中使用的所有组件。 All these components need to be loaded into entryComponents from my main component.
所有这些组件都需要从我的主要组件加载到entryComponents中。 I want to load the array of components from the service, into the decorator of the main component.
我想将服务中的组件数组加载到主组件的装饰器中。
@Injectable() // This is the service, I want to call getComponents() later on.
export class Configuration {
modules = [
ChartModule
]
components = [
PiechartComponent
]
getModules(): NgModule[] {
return this.modules;
}
getComponents(): Component[] {
return this.components;
}
};
In the main component I want the following: 在主要组件中,我想要以下内容:
@Component({
selector: 'dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.css'],
entryComponents: Configuration.getComponents() // Here I call the service.
})
Please help! 请帮忙!
I have solved this issue by making a barrel and constant at once. 我已经通过立刻制作一个桶而不变来解决这个问题。 That way I don't have to import all my components in the main module (and other files) and I have access to all the arrays and configuration I want to do.
这样我就不必导入主模块(和其他文件)中的所有组件,并且我可以访问我想要做的所有数组和配置。
import { TestModule } from './widgets/testmodule/test.module';
import { TestComponent } from './widgets/testmodule/test.component';
Because I want to have all the modules and components in a separate configuration file, I put it in a barrel and constant at the same time. 因为我希望将所有模块和组件放在一个单独的配置文件中,所以我将它放在桶中并同时保持不变。 I import all the modules and components in one location.
我在一个位置导入所有模块和组件。
Now I will add all these modules and components to arrays. 现在我将所有这些模块和组件添加到数组中。 Later on I will use these arrays in the decorators of the main module and component.
稍后我会在主模块和组件的装饰器中使用这些数组。
export const initialization: ConfigurationConfig = {
modules: [
TestModule,
AnotherModule,
CarModule,
PocModule
],
entryComponents: Array<any> = [
TestComponent,
AnotherComponent,
CarComponent,
PocComponent
];
}
In my main module I can now import this constant with all the imports from the barrel. 在我的主模块中,我现在可以使用桶中的所有导入来导入此常量。
import * as Configuration from './configuration.barrel';
@Component({
selector: 'df-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.scss'],
entryComponents: Configuration.initialization.entryComponents
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.