【问题标题】:How to include module in Angular 2如何在 Angular 2 中包含模块
【发布时间】:2016-09-01 22:45:41
【问题描述】:

我是 Angular 2 的新手,我正在学习“英雄”教程。
我需要导入这些模块:

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule }   from '@angular/forms';

我开始使用 Angular2 CLI(创建应用程序的简单方法),但现在当我尝试导入这些时,出现如下错误:

Error: Typescript found the following errors:"...node_modules/@angular/core/index"' has no exported member 'NgModule'.

我怎样才能包含它?

【问题讨论】:

  • 确保您拥有最新的@angular/core
  • 是的,请检查您的package.json 并查看@angular/core 的版本是否至少为rc.5

标签: angular angularjs-ng-model angular-cli


【解决方案1】:

假设你通过npm install -g angular-cli安装了angular-cli

该版本的 CLI 仍然基于 rc4,如果您想使用更新的(当前)版本,您需要通过以下方式安装它:

npm uninstall -g angular-cli
npm cache clean
npm install -g angular-cli@webpack

完成此操作后,您可以重新创建项目并使用上面的代码完成英雄之旅演示。

【讨论】:

    【解决方案2】:

    您的语法看起来是正确的。您需要将这些包含在导入元数据数组中。 NgModule 使用包含导入数组的 @NgModule 装饰器。你像这样导入它们:

    import { NgModule }      from '@angular/core';
    import { BrowserModule } from '@angular/platform-browser';
    import { FormsModule }   from '@angular/forms';
    
    @NgModule({
        imports: [
            BrowserModule,
            FormsModule,
        ],
    

    另外,当你检查你的 package.json 时,确保这些都包含在你的依赖中:

    "@angular/common": "2.0.0-rc.5",
        "@angular/compiler": "2.0.0-rc.5",
        "@angular/core": "2.0.0-rc.5",
        "@angular/forms": "0.3.0",
        "@angular/http": "2.0.0-rc.5",
        "@angular/platform-browser": "2.0.0-rc.5",
        "@angular/platform-browser-dynamic": "2.0.0-rc.5",
        "@angular/router": "3.0.0-rc.1",
    

    更新您的 package.json 后,运行“npm install”进行更新。

    【讨论】:

      猜你喜欢
      • 2017-10-26
      • 2016-08-23
      • 2018-02-24
      • 2017-03-12
      • 2013-10-28
      • 2018-02-12
      • 2018-03-24
      • 2017-06-20
      • 1970-01-01
      相关资源
      最近更新 更多