Angular Material 2 md-input-container 不工作

Angular Material 2 md-input-container not working

我在尝试使用输入时遇到 angular-material2 问题。但是,none 我尝试的方法有效。

我遇到了这个问题:

'md-input-container' is not a known element

HTML:

<md-input-container>
<input mdInput placeholder="Favorite food" value="Sushi">
</md-input-container>

我在 component.tsapp.ts:

中导入了这个
import { MdInputModule } from '@angular/material';

并且我在 app.ts

中的@NgModule 中的 'imports' 中声明了它

我的material版本是@angular/material@2.0.0-beta.8

感谢您的帮助。

编辑:我发现我在 app.module.ts 中导入了它,它导入了另一个我需要放置声明的模块。我的代码有点乱,我需要清理一下。对不起我的朋友们。案件结案。

import { MdInputModule } from '@angular/material';

并在imports: [ MdInputModule ]中添加MdInputModuleapp.ts中添加@NgModule

将 Material 模块导入您的应用程序以访问 material

的核心指令
import { MaterialModule } from '@angular/material';

您应该将 MdInputModule 放入 app.module.ts 的导入中。 你还应该导入你使用的Material元素的每个模块

您的做法是正确的,只导入您使用的组件的模块。如果您只使用一个或两个组件,则导入整个 MaterialModule 是多余的。 您需要确定的是(来自Getting Started guide, step 3):

Whichever approach you use, be sure to import the Angular Material modules after Angular's BrowserModule, as the import order matters for NgModules.

而不是 md-input-container 或 mat-input-container,使用 mat-form-field。 作为 8.0.0 版本当前支持的输入容器。有关详细信息,请参阅 official angular material doc