Angular2: ngModule, BrowserModule, FormsModule are

2020-03-01 03:00发布

问题:

I am going through the Angular2 tutorial at https://angular.io/docs/ts/latest/tutorial/toh-pt5.html. All good to step Routing. Visual Studio Code shows the error at first 03 lines:

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

as

[ts] 
Module '"c:/Users/ktran/Source/Repos/angular2-quickstart-ts/node_modules/@angular/core/index"' has no exported member 'NgModule'.

folder node_modules:

any idea please?

回答1:

Some modules just get added to the new version of Angular, so if you are not updating, you can not import it.

After about half an hour of search, got a solution:

Create a new folder and cd to your folder.

In command line, Type:

 git clone  https://github.com/angular/quickstart
 cd quickstart
 npm install

And copy your old code to the newly created project



回答2:

As Kim Phung stated, this is because a new RC of Angular was just released. Change the following lines in packages.json:

// ...snip...
"dependencies": {
    "@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",

// ...file continues...

Then run, in your console:

npm update

Good to go!



回答3:

If this solutions didn't help you, try to reopen VSCode. For me it was editor issue.



回答4:

if you have this type of error. import { NgModule} from '@angular/core'; import {BrowserModule} from '@angular/platform-browser'; try this command:

npm update



回答5:

I just had a similar issue upgrading a sample app to 2.0.1 using the quickstart sample. I fixed it by updating not only the package.json, but also systemjs.config.js and typings.json



回答6:

Please make sure you have included the FormsModule to the ngModule imports

@NgModule({
    imports:      [ BrowserModule,**FormsModule** ],
    declarations: [ AppComponent ],
    bootstrap:    [ AppComponent ]
})


回答7:

You must import this symbol in the module file. I am pretty sure that you are importing it in the component file.



标签: angular