Angular 2:TypeError:core_1.NgModule不是一个函数。

3
我正在学习这里的教程:

https://www.angular-meteor.com/tutorials/socially/angular2/search-sort-pagination-and-reactive-vars

我正在实现分页,但是出现了错误。控制台在parties-list.component.ts的第7行抛出了一个错误。第7行的代码如下:
import { PaginationService, PaginationControlsCmp } from 'ng2-pagination'; 

完整的错误信息如下:

 ng2-pagination.js:26Uncaught TypeError: core_1.NgModule is not a function

我认为ng2-pagination可能与我使用的Angular版本不兼容,但不确定如何确认,因为我正在Meteor内运行。

我的package.js文件:

{
  "name": "angular2-meteor-base",
  "private": true,
  "scripts": {
    "start": "meteor run",
    "test": "meteor test --driver-package practicalmeteor:mocha",
    "test:ci": "meteor test --once --driver-package dispatch:mocha-phantomjs"
  },
  "devDependencies": {
    "chai": "3.5.0",
    "chai-spies": "0.7.1"
  },
  "dependencies": {
    "@angular/common": "2.0.0-rc.4",
    "@angular/compiler": "2.0.0-rc.4",
    "@angular/core": "2.0.0-rc.4",
    "@angular/forms": "0.2.0",
    "@angular/platform-browser": "2.0.0-rc.4",
    "@angular/platform-browser-dynamic": "2.0.0-rc.4",
    "@angular/router": "3.0.0-alpha.8",
    "angular2-meteor": "0.6.2",
    "angular2-meteor-accounts-ui": "^0.6.1",
    "angular2-meteor-auto-bootstrap": "0.6.0",
    "angular2-meteor-polyfills": "0.1.1",
    "angular2-meteor-tests-polyfills": "0.0.2",
    "es6-shim": "0.35.1",
    "meteor-node-stubs": "0.2.3",
    "ng2-pagination": "https://registry.npmjs.org/ng2-pagination/-/ng2-pagination-0.4.1.tgz",
    "reflect-metadata": "0.1.3",
    "rxjs": "5.0.0-beta.6",
    "zone.js": "0.6.12"
  }
}

1
你想得很对 - 你的 ng2-bootstrap 版本与你的 Angular 2 版本不兼容,我也遇到了同样的问题。我建议你将所有的 Angular 2 包和 ng2-bootstrap 更新到最新版本。 - Stefan Svrkota
@StefanSvrkota,有没有简单的方法可以做到这一点? - Mike6679
我已经添加了我的package.js文件。我正在meteor内运行,所以我不确定应该更新哪些packages。 - Mike6679
但是,考虑到您仍在使用RC4,您需要重构您的应用程序,因为模块已经添加到RC5中。您可以在此处阅读更多信息:https://angular.io/docs/ts/latest/guide/ngmodule.html 关于迁移到模块的内容,请参阅:https://angular.io/docs/ts/latest/cookbook/rc4-to-rc5.html - Stefan Svrkota
我尝试更新所有的@angular依赖项,但一切都崩溃了 --- 我想Meteor可能依赖于某个特定版本的Angular,但是为什么这个分页步骤会在教程中呢?..嗯。 - Mike6679
显示剩余2条评论
1个回答

2
ng2-pagination包自0.4.0版本起(已于2016年9月9日在npm上发布),使用了新的NgModule(在angular2 RC5中引入,正如@StefanSvrkota在问题评论中所强调的)。请注意,此包独立于您正在使用的Angular2-meteor教程进行更新。
简单来说,包作者在其最新版本中引入了与angular2版本的同行依赖性兼容性问题。
如果您使用包的以前版本,在NgModule尚未使用时,则不会有问题:
$ npm uninstall --save ng2-pagination
$ npm install --save ng2-pagination@~0.3.0 # allow only patch version update

这应该给你版本 < 0.4.0,即目前的0.3.5

没有起作用...但现在出现了不同的错误:modules.js: _angular_core.createPlatformFactory不是一个函数。 - Mike6679
最后的错误是由于我模块的状态不佳,这是由于所有的npm安装和卸载导致的。我最终只是将ng2-pagination移除了,现在一切都恢复正常了。我会尝试使用另一个分页模块或自定义组件,以获得更兼容性的效果。 - Mike6679
“ng2-pagination” 的 0.3.5 版本对我来说没有问题。你可以尝试重新安装所有的 node_modules(只需删除整个 node_modules 文件夹并再次执行 npm install)。 - ghybs
我尝试删除node_modules下的所有内容,然后运行npm install... 仍然出现相同的问题:_angular_core.NgModule不是一个函数。在我的package.json中: "ng2-pagination": "^0.3.5"。 - Mike6679
实际上,我不认为这还是ng2-pagination的问题了...我现在看到它在另一个文件中抛出错误了(toolbar.umd.js)。在我的package.json文件中,这是依赖项:@angular2-material/toolbar": "^2.0.0-alpha.6"。 - Mike6679

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接