如何有条件地导入角模块内的组件?

发布于 2025-02-05 14:58:26 字数 1838 浏览 2 评论 0原文

我正在研究一个角度应用,我需要在条件的基础上导入组件。我可以通过在浏览器上渲染的其他方法来实现此目标我构建了这个,然后将其插入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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文