【问题标题】:'mat-checkbox' is not a known element:\'mat-checkbox\' 不是已知元素:
【发布时间】:2023-01-10 12:26:04
【问题描述】:

我正在尝试使用材料角度的复选框,但它不起作用。我将逐步展示我在做什么:

**package.json**

    "dependencies": {
        "@angular/material": "^10.2.5",
    }
**app.module.ts**

import { MatSliderModule } from '@angular/material/slider';
import { MatCheckboxModule } from '@angular/material/checkbox';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations'
import { BrowserModule, Title } from '@angular/platform-browser'

@NgModule({
    imports: [
        MatCheckboxModule,
        MatSliderModule,
        BrowserModule,
        BrowserAnimationsModule,
    ],
    declarations: [AppComponent],
})
**app.component.html**

<mat-slider min="1" max="100" step="1" value="50"></mat-slider>
<mat-checkbox></mat-checkbox>

垫滑块工作正常,我也试过垫卡它工作正常,但与垫复选框我收到此错误:

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

我做错了什么?

【问题讨论】:

    标签: angular angular-material


    【解决方案1】:

    确保你已经导入了MatCheckboxModule

    import {MatCheckboxModule} from '@angular/material/checkbox';
    
    @NgModule({
        imports: [
            ..., // Other module imports
            MatCheckboxModule
        ],
        ...
    })
    

    【讨论】:

    • 是的,我已经导入了。对不起,我忘了把它放在这个例子中。你知道它会是什么吗?
    • 嗯,你有没有尝试重启 Angular 应用程序?你能尝试在 StackBlitz 中重现吗?
    • 我尝试在 StackBlitz 中重现,但效果很好。然后我在我的应用程序中复制/粘贴完全相同的代码,甚至是依赖项,但我仍然遇到相同的错误:/
    • @SirrMaxi 可以分享你的项目吗?我可以自己调试。
    • @HassanMoin 我修好了。问题是我使用的是 Angular v8 和材料 v12。所以我将材料降级到版本 8,它运行良好。谢谢您的帮助!
    【解决方案2】:

    我遇到了同样的问题,但解决方案相当简单:mat-checkbox 是复制组件的一部分,我忘记将复制组件添加到模块的声明部分。

    【讨论】:

      猜你喜欢
      • 2019-05-09
      • 2020-04-18
      • 1970-01-01
      • 1970-01-01
      • 2023-01-07
      • 2019-06-27
      • 2020-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多