【问题标题】:Angular - How to fix "ERROR TS2322: Type 'ModuleWithProviders<any>' is not assignable to type 'any[] | Type<any>'"Angular - 如何修复“错误 TS2322:类型 'ModuleWithProviders<any>' 不可分配给类型 'any[] | Type<any>'”
【发布时间】:2019-09-08 14:39:35
【问题描述】:

我正在尝试使用 Angular 创建一个 web 应用程序,但在编译时我收到了这个我无法解决的错误:

src/app/app.module.ts(22,5) 中的错误:错误 TS2322:类型 'ModuleWithProviders' 不可分配给类型 'any[] | 类型'。类型“ModuleWithProviders”缺少以下内容 来自“类型”类型的属性:应用、调用、绑定、原型和 5 更多。

那是我的 app.module.ts:

import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { RouterModule } from '@angular/router';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { ToastrModule } from 'ngx-toastr';
import { AppRoutingModule } from './app-routing.module';
import { ComponentsModule } from './components/components.module';
import { AppComponent } from './app.component';
import { AdminLayoutComponent } from './layouts/admin-layout/admin-layout.component';

@NgModule({
  declarations: [
    BrowserAnimationsModule,
    FormsModule,
    HttpClientModule,
    ComponentsModule,
    RouterModule,
    AppRoutingModule,
    NgbModule,
    ToastrModule.forRoot()
  ],
  imports: [
    AppComponent,
    AdminLayoutComponent
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

报错就行了:ToastrModule.forRoot()

这就是我的 package.json:

{
  "name": "pum-pum-pum",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~7.2.0",
    "@angular/common": "~7.2.0",
    "@angular/compiler": "~7.2.0",
    "@angular/core": "~7.2.0",
    "@angular/forms": "~7.2.0",
    "@angular/platform-browser": "~7.2.0",
    "@angular/platform-browser-dynamic": "~7.2.0",
    "@angular/router": "~7.2.0",
    "core-js": "^2.5.4",
    "rxjs": "~6.3.3",
    "tslib": "^1.9.0",
    "zone.js": "~0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.13.0",
    "@angular/cli": "~7.3.7",
    "@angular/compiler-cli": "~7.2.0",
    "@angular/language-service": "~7.2.0",
    "@types/node": "~8.9.4",
    "@types/jasmine": "~2.8.8",
    "@types/jasminewd2": "~2.0.3",
    "codelyzer": "~4.5.0",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~4.0.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~1.1.2",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.11.0",
    "typescript": "~3.2.2"
  }
}

请有人帮助我。

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

    如我所见,您在声明下添加了模块,在导入下添加了组件,您应该在导入下添加模块,在声明下添加组件。

    【讨论】:

    • 能否请您添加对官方文档的引用?
    【解决方案2】:
    1. declarations 保存components,pipesdirectives
    2. imports 保存modules
    @NgModule({
      // right
      imports: [
        BrowserAnimationsModule,
        FormsModule,
        HttpClientModule,
        ComponentsModule,
        RouterModule,
        AppRoutingModule,
        NgbModule,
        ToastrModule.forRoot()
      ],
      // right
      declarations: [
        AppComponent,
        AdminLayoutComponent
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    export class AppModule { }
    

    【讨论】:

      猜你喜欢
      • 2022-07-28
      • 1970-01-01
      • 1970-01-01
      • 2021-04-22
      • 2017-08-12
      • 2022-08-04
      • 1970-01-01
      • 2021-11-05
      • 2018-04-23
      相关资源
      最近更新 更多