How to fix Error: Unexpected value 'DataTableModule' imported by the module 'DashboardModule'. Please add a @NgModule annotation
How to fix Error: Unexpected value 'DataTableModule' imported by the module 'DashboardModule'. Please add a @NgModule annotation
我将 Angular 4 项目转换为 Angular 7. ng serve 命令有效,但在登录应用程序后,它在控制台中给出错误 "Unexpected value 'DataTableModule' imported by the module 'DashboadModule' . Please add a @NgModule annotation." 应用程序在 Angular 4 版本中工作正常。我已经在 Imports 数组定义中有 DatatableModule 那么出现错误的原因是什么。
如果我构建 --prod。它给出以下错误 -
'DataTableModule' 的模板编译期间出现错误
'NgModule' 中的装饰器不支持函数调用
'NgModule' 在@angular\core\core.ts(194,50).
调用一个函数
这里DashboardModule是自定义模块,DataTableModule是Dashboard.module.ts中导入的NPM模块。下面是定义 -
1) dashboard.module.ts
import { NgModule } from '@angular/core';
import { DashboardComponent } from './dashboard.component';
import { DashboardRoutingModule } from './dashboard-routing.module';
import { SharedModule } from './../shared/shared.module';
import { DataTableModule } from 'angular-4-data-table-bootstrap-4';
import { CommonModule } from '@angular/common';
import { DashboardService } from './../shared/_services/dashboard.service';
@NgModule({
imports: [
SharedModule,CommonModule,DataTableModule,
DashboardRoutingModule
],
declarations: [
DashboardComponent
],
providers:[DashboardService]
})
export class DashboardModule { }
2) package.json 依赖项
这里我用于数据表的版本是 - "angular-4-data-table-bootstrap-4": "^0.2.0",
"dependencies": {
"@agm/core": "^1.0.0-beta.1",
"@angular/animations": "^7.2.14",
"@angular/common": "^7.2.14",
"@angular/compiler": "^7.2.14",
"@angular/core": "^7.2.14",
"@angular/forms": "^7.2.14",
"@angular/http": "^7.2.14",
"@angular/platform-browser": "^7.2.14",
"@angular/platform-browser-dynamic": "^7.2.14",
"@angular/router": "^7.2.14",
"@ng-bootstrap/ng-bootstrap": "^1.0.0-beta.5",
"angular-2-dropdown-multiselect": "^1.9.0",
"angular-4-data-table-bootstrap-4": "^0.2.0",
"angular2-multiselect-dropdown": "^2.10.2",
"classlist.js": "^1.1.20150312",
"core-js": "^2.5.0",
"file-saver": "^1.3.3",
"geolib": "^2.0.24",
"intl": "^1.2.5",
"moment": "^2.22.1",
"mydatepicker": "^2.6.3",
"ng-pick-datetime": "^5.0.0-beta.10",
"ng2-filter-pipe": "^0.1.10",
"ngx-chips": "^1.6.5",
"ngx-inactivity": "^1.0.1",
"ngx-loading": "^1.0.8",
"ngx-order-pipe": "^1.1.0",
"ngx-pagination": "^3.0.1",
"rxjs": "^6.5.1",
"rxjs-compat": "^6.5.1",
"web-animations-js": "^2.3.1",
"xlsx": "^0.14.3",
"xlsx-style": "^0.8.13",
"zone.js": "^0.9.1"
}
3) dashboard.component.ts
我从 DataTableModule 导入了以下定义
import { DataTable, DataTableResource, DataTableTranslations, DataTablePagination } from 'angular-4-data-table-bootstrap-4';
我还在 NPM 存储库中验证了它的最新版本 - https://www.npmjs.com/package/angular-4-data-table-bootstrap-4 它是最新的,自从过去 2 年以来,根本没有最新版本。我担心 Angular 7 关于模块声明或定义有什么重大变化吗?如果是,那些是什么?以及如何继续解决上述问题?
我认为这可行。
在app.module.ts中:
只需替换:
import { DataTableModule } from 'angular-4-data-table';
和
import { DataTableModule } from 'angular-4-data-table/src/index';
在 angular 11 中,使用 angular5-data-table,还在 polyfills.ts
文件的末尾附加以下内容
declare module "@angular/core" {
interface ModuleWithProviders<T = any> {
ngModule: Type<T>;
providers?: Provider[];
}
}
我将 Angular 4 项目转换为 Angular 7. ng serve 命令有效,但在登录应用程序后,它在控制台中给出错误 "Unexpected value 'DataTableModule' imported by the module 'DashboadModule' . Please add a @NgModule annotation." 应用程序在 Angular 4 版本中工作正常。我已经在 Imports 数组定义中有 DatatableModule 那么出现错误的原因是什么。
如果我构建 --prod。它给出以下错误 -
'DataTableModule' 的模板编译期间出现错误 'NgModule' 中的装饰器不支持函数调用 'NgModule' 在@angular\core\core.ts(194,50).
调用一个函数这里DashboardModule是自定义模块,DataTableModule是Dashboard.module.ts中导入的NPM模块。下面是定义 -
1) dashboard.module.ts
import { NgModule } from '@angular/core';
import { DashboardComponent } from './dashboard.component';
import { DashboardRoutingModule } from './dashboard-routing.module';
import { SharedModule } from './../shared/shared.module';
import { DataTableModule } from 'angular-4-data-table-bootstrap-4';
import { CommonModule } from '@angular/common';
import { DashboardService } from './../shared/_services/dashboard.service';
@NgModule({
imports: [
SharedModule,CommonModule,DataTableModule,
DashboardRoutingModule
],
declarations: [
DashboardComponent
],
providers:[DashboardService]
})
export class DashboardModule { }
2) package.json 依赖项
这里我用于数据表的版本是 - "angular-4-data-table-bootstrap-4": "^0.2.0",
"dependencies": {
"@agm/core": "^1.0.0-beta.1",
"@angular/animations": "^7.2.14",
"@angular/common": "^7.2.14",
"@angular/compiler": "^7.2.14",
"@angular/core": "^7.2.14",
"@angular/forms": "^7.2.14",
"@angular/http": "^7.2.14",
"@angular/platform-browser": "^7.2.14",
"@angular/platform-browser-dynamic": "^7.2.14",
"@angular/router": "^7.2.14",
"@ng-bootstrap/ng-bootstrap": "^1.0.0-beta.5",
"angular-2-dropdown-multiselect": "^1.9.0",
"angular-4-data-table-bootstrap-4": "^0.2.0",
"angular2-multiselect-dropdown": "^2.10.2",
"classlist.js": "^1.1.20150312",
"core-js": "^2.5.0",
"file-saver": "^1.3.3",
"geolib": "^2.0.24",
"intl": "^1.2.5",
"moment": "^2.22.1",
"mydatepicker": "^2.6.3",
"ng-pick-datetime": "^5.0.0-beta.10",
"ng2-filter-pipe": "^0.1.10",
"ngx-chips": "^1.6.5",
"ngx-inactivity": "^1.0.1",
"ngx-loading": "^1.0.8",
"ngx-order-pipe": "^1.1.0",
"ngx-pagination": "^3.0.1",
"rxjs": "^6.5.1",
"rxjs-compat": "^6.5.1",
"web-animations-js": "^2.3.1",
"xlsx": "^0.14.3",
"xlsx-style": "^0.8.13",
"zone.js": "^0.9.1"
}
3) dashboard.component.ts
我从 DataTableModule 导入了以下定义
import { DataTable, DataTableResource, DataTableTranslations, DataTablePagination } from 'angular-4-data-table-bootstrap-4';
我还在 NPM 存储库中验证了它的最新版本 - https://www.npmjs.com/package/angular-4-data-table-bootstrap-4 它是最新的,自从过去 2 年以来,根本没有最新版本。我担心 Angular 7 关于模块声明或定义有什么重大变化吗?如果是,那些是什么?以及如何继续解决上述问题?
我认为这可行。
在app.module.ts中:
只需替换:
import { DataTableModule } from 'angular-4-data-table';
和
import { DataTableModule } from 'angular-4-data-table/src/index';
在 angular 11 中,使用 angular5-data-table,还在 polyfills.ts
文件的末尾附加以下内容
declare module "@angular/core" {
interface ModuleWithProviders<T = any> {
ngModule: Type<T>;
providers?: Provider[];
}
}