如何有条件地导入角模块内的组件?
我正在研究一个角度应用,我需要在条件的基础上导入组件。我可以通过在浏览器上渲染的其他方法来实现此目标我构建了这个,然后将其插入DOM。有人可以帮我吗?这是我的代码。
home.module.ts
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { HomeComponent } from './home.component';
import { AboutUsComponentA } from './about-us-A.component';
import { AboutUsComponentB } from './about-us-B.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
const isAboutusA = true
@NgModule({
declarations: [
HomeComponent,
CollectionHomeComponent,
BlogSectionComponenet,
isAboutusA === true ? AboutUsAComponent: AboutUsBComponent,// I need to achieve this, in this way this is not working.
],
imports: [
CommonModule,
BrowserAnimationsModule
],
providers: []
})
export class HomeModule { }
关于usaComponent
import { Component } from ‘@angular/core’;
@Component({
template: ‘<div>This is the AboutUs A Component </div>’,
styles: [‘:host {color: blue}’]
})
export class AboutUsAComponent {}
关于usbcomponent
import { Component } from ‘@angular/core’;
@Component({
template: ‘<div>This is the About Us B Component </div>’,
styles: [‘:host {color: blue}’]
})
export class AboutUsBComponent {}
homecoment
import { Component } from ‘@angular/core’;
@Component({
template: ‘<div>This is the Home component
<AboutUsAComponent></AboutUsAComponent>
<AboutUsBComponent></AboutUsBComponent>
</div>’,
styles: [‘:host {color: blue}’]
})
export class HomeComponent {}
I am working on a angular app where I need to import the components on the basis of condition.I can achieve this by other methods which renders any one when it gets rendered on browser.But I need to get loaded any one of the component when I build this and before it gets inserted in DOM. Can anyone please help me with this? Here is my code.
Home.module.ts
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { HomeComponent } from './home.component';
import { AboutUsComponentA } from './about-us-A.component';
import { AboutUsComponentB } from './about-us-B.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
const isAboutusA = true
@NgModule({
declarations: [
HomeComponent,
CollectionHomeComponent,
BlogSectionComponenet,
isAboutusA === true ? AboutUsAComponent: AboutUsBComponent,// I need to achieve this, in this way this is not working.
],
imports: [
CommonModule,
BrowserAnimationsModule
],
providers: []
})
export class HomeModule { }
AboutUsAComponent
import { Component } from ‘@angular/core’;
@Component({
template: ‘<div>This is the AboutUs A Component </div>’,
styles: [‘:host {color: blue}’]
})
export class AboutUsAComponent {}
AboutUsBComponent
import { Component } from ‘@angular/core’;
@Component({
template: ‘<div>This is the About Us B Component </div>’,
styles: [‘:host {color: blue}’]
})
export class AboutUsBComponent {}
HomeComponent
import { Component } from ‘@angular/core’;
@Component({
template: ‘<div>This is the Home component
<AboutUsAComponent></AboutUsAComponent>
<AboutUsBComponent></AboutUsBComponent>
</div>’,
styles: [‘:host {color: blue}’]
})
export class HomeComponent {}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论