gpt4 book ai didi

Angular 6 错误显示为 'mat-form-field' 不是已知元素 :

转载 作者:太空狗 更新时间:2023-10-29 17:11:19 26 4
gpt4 key购买 nike

您好,我正在使用 angular 6,我的代码如下:

import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule, Routes } from '@angular/router';




import { AppComponent } from './app.component';
import { AppRoutingModule } from './app.routing.module';


import { MatButtonModule, MatFormFieldModule, MatInputModule, MatRippleModule } from '@angular/material';
//import { MaterialModule } from 'src/app/et-shared/material.module';


const modules = [
MatButtonModule,
MatFormFieldModule,
MatInputModule,
MatRippleModule
];

@NgModule({
declarations: [
AppComponent,
],
imports: [
BrowserModule,
BrowserAnimationsModule,
CommonModule,
RouterModule,
AppRoutingModule,
// MaterialModule,
...modules

],
exports:[
...modules
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }

和这样的html

<div class="example-container">
<mat-form-field>
<input matInput placeholder="Input">
</mat-form-field>

<mat-form-field>
<textarea matInput placeholder="Textarea"></textarea>
</mat-form-field>

<mat-form-field>
<mat-select placeholder="Select">
<mat-option value="option">Option</mat-option>
</mat-select>
</mat-form-field>
</div>

我的包裹是这样的

"dependencies": {
"@angular/animations": "^6.0.0",
"@angular/cdk": "^6.0.1",
"@angular/common": "^6.0.0",
"@angular/compiler": "^6.0.0",
"@angular/core": "^6.0.0",
"@angular/forms": "^6.0.0",
"@angular/http": "^6.0.0",
"@angular/material": "^6.0.1",
"@angular/platform-browser": "^6.0.0",
"@angular/platform-browser-dynamic": "^6.0.0",
"@angular/router": "^6.0.0",
"core-js": "^2.5.4",
"hammerjs": "^2.0.8",
"rxjs": "^6.0.0",
"zone.js": "^0.8.26"
},

我遇到了这个错误

'mat-form-field' is not a known element:
1. If 'mat-form-field' is an Angular component, then verify that it is part of this module.
2. If 'mat-form-field' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("

<div class="example-container">
[ERROR ->]<mat-form-field>
<input matInput placeholder="Input">
</mat-form-field>
"): ng:///AppRoutingModule/LoginComponent.html@7:2
'mat-form-field' is not a known element:
1. If 'mat-form-field' is an Angular component, then verify that it is part of this module.
2. If 'mat-form-field' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("
</mat-form-field>

[ERROR ->]<mat-form-field>
<textarea matInput placeholder="Textarea"></textarea>
</mat-form-field>
"): ng:///AppRoutingModule/LoginComponent.html@11:2
'mat-option' is not a known element:
1. If 'mat-option' is an Angular component, then verify that it is part of this module.
2. If 'mat-option' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("
<mat-form-field>
<mat-select placeholder="Select">
[ERROR ->]<mat-option value="option">Option</mat-option>
</mat-select>
</mat-form-field>
"): ng:///AppRoutingModule/LoginComponent.html@17:6
'mat-select' is not a known element:
1. If 'mat-select' is an Angular component, then verify that it is part of this module.
2. If 'mat-select' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("

为什么我会遇到这个错误我不得不花太多时间来解决这个问题但在我错的地方没有得到任何解决方案我也搜索堆栈溢出在这里一些找到解决方案但没有解决我的问题你能帮忙吗我发现了这个错误

谢谢你解决了我的问题

最佳答案

查看您的错误 ng:///AppRoutingModule/LoginComponent.html@11:2

我可以得出结论,您在 AppRoutingModule 中声明了 LoginComponent 但没有在那里导入 MatFormFieldModule

LoginComponent 移动到 AppModuledeclarations 数组中:

@NgModule({
declarations: [
AppComponent,
LoginComponent
],
...
})
export class AppModule { }

或在 AppRoutingModule 中导入 MatFormFieldModule 或一些 SharedModule:

@NgModule({
declarations: [
LoginComponent,
...
],
imports: [
MatFormFieldModule // or SharedModule that exports MatFormFieldModule
...
]
...
})
export class AppRoutingModule { }

另见:

关于Angular 6 错误显示为 'mat-form-field' 不是已知元素 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50328751/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com