如果 mat-form-field 在组件中,则表单布局会中断

Form layout breaks if mat-form-field is in a component

如果我直接在表单中使用mat-form-fields,字段显示在"column"流程中,如下

<h1>Form 1</h1>
<form class="example-form">
  <mat-form-field >
  <input matInput  placeholder='Name' value='world' readonly='true'>
</mat-form-field>
 <mat-form-field >
  <input matInput  placeholder='Name' value='world' readonly='true'>
</mat-form-field>
 <mat-form-field >
  <input matInput  placeholder='Name' value='world' readonly='true'>
</mat-form-field>
 <mat-form-field >
  <input matInput  placeholder='Name' value='world' readonly='true'>
</mat-form-field>
</form>

但是,如果将 mat-form-fields 移动到 angular 组件,这些字段将以行布局显示

<h1>Form 2</h1>
<form class="example-form1">
  <input-field></input-field>
  <input-field></input-field>
  <input-field></input-field>
  <input-field></input-field>
</form>

其中输入字段组件的模板定义如下:

<mat-form-field >
  <input matInput  placeholder='Name' value="world" readonly='true'>
</mat-form-field>

这就是最终结果。 Form1(无 angular 组件)和 Form 2(字段 angular 组件)

https://stackblitz.com/edit/angular-b3rxbf?embed=1&file=app/input-field.html

我应该对组件的 CSS 做任何更改吗?

您的第一个表单有 max-width: 500px;,强制输入换行。第二种形式采用所有可用的 space,恰好足以让所有字段适合一行。