您可以使用 ngSwitchCase,Dom 会自动重新渲染,这是一个很好的衡量标准。或使用 ngClass 并使用简单的布尔逻辑将元素绑定到不同的位置,还有其他更复杂的方法可以做到这一点,但如果它只有几个位置,那么对于代码维护问题来说,更简单地做到这一点更有意义。使用 switchcase 只允许根据条件重新渲染 Dom。您还可以使用带有指令的 ngOnInit、ngOnDestory 来生成组件,这就是我们所做的 - 但如果您没有,那就需要维护很多代码。
yourloading.component.html
<ng-container >
<div [ngSwitch]="deviceType">
<div *ngSwitchCase="'desktop'"> <app-component1 [layout]="desktop"></app-component1></div></ng-container>
</div>
<div *ngSwitchCase="'mobile'"> <app-component1 [layout]="mobile"></app-component1></div></ng-container>
</div>
</div>
</ng-container>
yourloading.component.ts
在您打开组件装饰器之后
deviceType ?: string;
component1.component.ts
//import Input
import { Component, OnInit, Input } from '@angular/core';
@Input() layOut ?: string;
topLocation: boolean;
bottomLocation: boolean;
ngOnInit(){
if(layout === 'desktop'){
topLocation = true;
}
if(layout === 'mobile'){
bottomLocation = true;
}
}
component1.component.css
.top{
margin-top: 0;
height: 100vh;
width: 100vw;
}
.bottom {
margin-bottom: 0;
bottom: 0;
height: 100vh;
width: 100vw;
}
.show{
visibility: visible;
height: 100%;
}
.hide{
visibility: none;
height: 0;
}
component1.component.html
<div [ngClass]="topLocation ? 'show':'hide'">
component html cool things maybe call another component with designs or something or change other css classes
</div>
<div [ngClass]="bottomLocation ? 'show':'hide'">
component html cool things
</div>