typescript Angular:组件没有提供程序,[多个Angular HTML元素]不是已知元素

hyrbngr7  于 2023-10-22  发布在  TypeScript
关注(0)|答案(1)|浏览(120)

我目前遇到以下单元测试失败:
'mat-card' is not a known element (used in the 'ChatCardComponent' component template):
'app-card' is not a known element (used in the 'DashboardComponent' component template)
R3InjectorError(DynamicTestModule)[DashboardComponent -> DashboardComponent]: NullInjectorError: No provider for DashboardComponent!
R3InjectorError(DynamicTestModule)[UserService -> HttpClient -> HttpClient]: NullInjectorError: No provider for HttpClient!
'mat-card' is not a known element (used in the 'CardComponent' component template):
'app-head-bar' is not a known element (used in the 'HomePageComponent' component template)
我觉得这很奇怪,因为我已经声明并导入了据称从我的app. module. ts中丢失的所有内容。我没有其他模块文件。

import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {AppRoutingModule} from './app-routing.module';
import {AppComponent} from './app.component';
import {HeadBarComponent} from './head-bar/head-bar.component';
import {DashComponent} from './dash/dash.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { DashboardComponent } from './card/dashboard/dashboard.component';
import { MatGridListModule } from '@angular/material/grid-list';
import { MatCardModule } from '@angular/material/card';
import { MatMenuModule } from '@angular/material/menu';
import { MatIconModule } from '@angular/material/icon';
import { MatButtonModule } from '@angular/material/button';
import { CardComponent } from './card/card.component';
import { KlausurenCardComponent } from './card/klausuren-card/klausuren-card.component';
import { ChatCardComponent } from './card/chat-card/chat-card.component';
import { HausaufgabenCardComponent } from './card/hausaufgaben-card/hausaufgaben-card.component';
import { LoginPageComponent } from './user/login-page/login-page.component';
import { HomePageComponent } from './card/home-page/home-page.component';
import { HttpClientModule } from "@angular/common/http";
import { SettingsComponent } from './settings/settings.component';
import {NgOptimizedImage} from "@angular/common";

@NgModule({
    declarations: [
        AppComponent,
        HeadBarComponent,
        DashComponent,
        DashboardComponent,
        CardComponent,
        KlausurenCardComponent,
        ChatCardComponent,
        HausaufgabenCardComponent,
        LoginPageComponent,
        HomePageComponent,
        SettingsComponent
    ],
    imports: [
        BrowserModule,
        AppRoutingModule,
        BrowserAnimationsModule,
        MatGridListModule,
        MatCardModule,
        MatMenuModule,
        MatIconModule,
        HttpClientModule,
        MatButtonModule,
        NgOptimizedImage
    ],
    providers: [],
    bootstrap: [AppComponent]
})
export class AppModule {
}

我正在尝试为我的Angular前端运行Karma测试,我已经尝试重新安装Angular Materials,认为安装可能已经失败,但没有任何结果。我仍然得到这些错误。问题出在哪里?
在此先谢谢您!

cgvd09ve

cgvd09ve1#

您是否在测试中导入AppCard和MatCard模块?如果没有尝试添加

import { MatCardModule } from '@angular/material/card';
import { CardComponent } from './card/card.component';

在你的测试中

相关问题