角4:如果有多个具有相同选择器的组件,如何根据某些条件仅加载一个组件? [英] Angular 4 : If there are multiple components with same selector, how to load only one component based on some condition?

查看:62
本文介绍了角4:如果有多个具有相同选择器的组件,如何根据某些条件仅加载一个组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

主要组件:

@Component({
    selector: 'app-root',
    template: '<app-report></app-report>'
})

组件1:

@Component({
    selector: 'app-report',
    templateUrl: './trip.report.html'
})

Component2:

Component2 :

@Component({
    selector: 'app-report',
    templateUrl: './user.report.html'
})

角度4中是否有任何方式可以根据某种条件根据某种条件将哪个组件(组件1或2)加载到主组件"中?

Is there any way in angular 4, through which based on some condition which component (component 1 or 2) will be loaded in "main component" based on some condition?

推荐答案

没有办法做到.对于在内部声明并导入到单个NgModule中的所有组件和指令,选择器必须唯一.

There is no way to do that. Selectors need to be unique for all components and directives declared within and imported into a single NgModule.

如果您拥有代码,请不要创建冲突的选择器.

If you own the code, just don't create conflicting selectors.

如果导入的第三方模块具有选择器冲突的组件,
您可以使用不冲突的选择器将冲突的组件包装在另一个组件中,并将其添加到其自己的模块中. 然后,您可以在@NgModule({imports: [...]})中指定应使用哪个组件.

If imported 3rd-party modules have components with conflicting selectors,
you can wrap a conflicting component in another component with a non-conflicting selector and add it to it's own module. Then you can specify in @NgModule({imports: [...]}) which component should be used.

然后您就可以使用*ngIf

<conflicting-component *ngIf="foo"></conflicting-component>
<de-conflicted-wrapper *ngIf="!foo"></de-conflicted-wrapper>

这篇关于角4:如果有多个具有相同选择器的组件,如何根据某些条件仅加载一个组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆