简体   繁体   English

如何在 Web 应用程序中并排放置 Angular 组件

[英]How to place Angular components side by side in web app

I'm trying to create a Web-app using Angular where a user can play a card game.我正在尝试使用 Angular 创建一个 Web 应用程序,用户可以在其中玩纸牌游戏。 A list of possible players should be depicted on the left, and the playing table should be to the right of the players list.左侧应显示可能的玩家列表,游戏桌应位于玩家列表的右侧。

Example of desired layout所需布局的示例

However, since I'm using Angular and the players list and the playing table are different components, first the players list is shown and below that the playing table is located.但是,由于我使用的是 Angular 并且玩家列表和游戏桌是不同的组件,因此首先显示玩家列表,然后显示在游戏桌的下方。 What it looks like now现在的样子

My app.components.html looks like this:我的 app.components.html 看起来像这样:

<h1>{{title}}</h1>
<app-pippelaars></app-pippelaars> <!--component displaying available players-->
<app-playing-table></app-playing-table> <!--component displaying playing table-->

How do I get these components to be next to each other in the web app instead of one above the other?如何让这些组件在 Web 应用程序中彼此相邻而不是一个在另一个之上?

Since 2017 all major browsers support CSS Grid layout , you can use that to position your components correctly.自 2017 年以来,所有主要浏览器都支持CSS 网格布局,您可以使用它来正确定位您的组件。 To change the size of the colums you can use the ' grid-template-columns ' property and pass a fixed px value or use fractional units for a responsive layout .要更改列的大小,您可以使用“ grid-template-columns ”属性并传递固定的 px 值或使用小数单位进行响应式布局

HTML HTML

<div class="grid-container">
  <app-pippelaars></app-pippelaars>
  <app-playing-table></app-playing-table>
</div>

CSS CSS

.grid-container {
   display: grid;
   grid-template-areas: "players-list  playing-table";
   grid-template-columns: 1fr 3fr;
}

 app-pippelaars { grid-area: players-list}
 app-playing-table { grid-area: playing-table}

you can try with flex layout, something like that:您可以尝试使用 flex 布局,例如:

in your scss file:在您的 scss 文件中:

:host {
 display: flex;
 width: 100%;
 
 app-pippelaars {
  width: 300px
 } 

 app-playing-table {
  flex: 1;
 }
}

more information and example of flex layout can you read here您可以在此处阅读更多信息和 flex 布局示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM