mat-datepicker 使应用程序崩溃而不记录错误

mat-datepicker 使应用程序崩溃而不记录错误

问题描述:

我在 url /demo

<mat-form-field>
  <input matInput placeholder="Choose a date">
</mat-form-field>

渲染得很好,但是一旦我将其重写为

Which renders fine, but as soon as I rewrite this as

<mat-form-field>
  <input matInput [matDatepicker]="picker" placeholder="Choose a date">
  <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
  <mat-datepicker #picker></mat-datepicker>
</mat-form-field>

应用程序重定向到 url / 并且除了空格之外不显示任何内容.控制台中没有记录任何内容.

the app redirects to the url / and does not displays anything but blank space. Nothing is logged in the console.

代码应该可以工作,因为它是从 Angular Material 基本日期选择器示例.我已经导入了 MatFormFieldModuleMatInputModuleMatDatepickerModule.

The code is supposed to work because it's copied from Angular Material basic datepicker example. I've imported MatFormFieldModule, MatInputModule and MatDatepickerModule.

发生了什么?

一个解决方案是额外导入可选的 Material 模块 MatNativeDateModule.Material's doc on mat-datepicker 中没有明确说明依赖关系,但在某些情况下似乎是必需的.

A solution is to additionally import the optional Material module MatNativeDateModule. No dependency is explicitly stated in Material's doc on mat-datepicker, but it seems to be required in some cases.

感谢 @pengyy这个问题帮助我找到了这个解决方案.

Thanks to @pengyy 's answer on this question that helped me get to this solution.