Cannot find module 'angular2/angular2'

后端 未结 11 856
有刺的猬
有刺的猬 2020-11-30 06:43

I am developing an node app with angular2 and gulp. I have written a component file login.ts as follows:

import {Component, View} from \'angular2/angular2\';         


        
相关标签:
11条回答
  • 2020-11-30 07:14

    As per the new release of Angular2 rc1, you can update your package.json to

    "dependencies": {
        "@angular/common": "2.0.0-rc.1",
        "@angular/compiler": "2.0.0-rc.1",
        "@angular/core": "2.0.0-rc.1",
        "@angular/http": "2.0.0-rc.1",
        "@angular/platform-browser": "2.0.0-rc.1",
        "@angular/platform-browser-dynamic": "2.0.0-rc.1",
        "@angular/router": "2.0.0-rc.1",
        "@angular/router-deprecated": "2.0.0-rc.1",
        "@angular/upgrade": "2.0.0-rc.1",
        .....
     }
    

    In addition to this also import as following in your component or container:

    import { Component } from '@angular/core';
    import {ROUTER_DIRECTIVES, Router, RouteParams} from '@angular/router-deprecated';
    
    0 讨论(0)
  • 2020-11-30 07:15

    import from ''angular2/angular2' was in previous version which no longer supported now .So Now we have to import the same from 'angular2/core'.For detail reference use (https://angular.io/guide/quickstart)

    0 讨论(0)
  • 2020-11-30 07:16

    'angular2/angular2' is not a valid dependencies of angular2

    you have to first check package.json file "@angular/core" exist or not

    "dependencies": {
        "@angular/common": "2.0.0",
        "@angular/compiler": "2.0.0",
        "@angular/core": "2.0.0",
        "@angular/http": "2.0.0",
        "@angular/platform-browser": "2.0.0",
        "@angular/platform-browser-dynamic": "2.0.0",
        "@angular/router": "2.0.0",
        "@angular/router-deprecated": "2.0.0",
        "@angular/upgrade": "2.0.0",
        .....
     }
    

    see the component file like this and also you have too use formGroup as belove

        import { Component, OnInit, DoCheck } from '@angular/core'
        import { Router } from '@angular/router'
    
        import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms'
    
        @Component({
          selector: 'app-user-profile',
          templateUrl: './user-profile.component.html',
          styleUrls: ['./user-profile.component.scss'],
        })
        export class UserProfileComponent implements OnInit, DoCheck {
          profileForm: FormGroup
    
          constructor(private fb: FormBuilder) {
            this.profileForm = this.fb.group({
              firstName: ['', Validators.required],
              lastName: ['', Validators.required],
              email: ['', Validators.required],
              mobileNo: ['', Validators.required]
            });
        }
    

    than you have to import ReactiveFormsModule in app.module.ts file
    import { ReactiveFormsModule } from '@angular/forms';

    without ReactiveFormsModule formGroup not work it make error

    import { NgModule } from '@angular/core';
    import { CommonModule } from '@angular/common';
    import { UserProfileComponent } from './user-profile.component';
    
    import { UserProfileRoutingModule } from './user-profile-routing.module';
    import { ReactiveFormsModule } from '@angular/forms';
    
    @NgModule({
      imports: [
        CommonModule,
        UserProfileRoutingModule,
        ReactiveFormsModule
      ],
      declarations: [UserProfileComponent]
    })
    
    0 讨论(0)
  • 2020-11-30 07:24

    yups as said by @simon error is in imports. but for further imports i have posted this answer may be this is useful for others too.

    import {Component, View, Directive, Input, Output, Inject, Injectable, provide} from 'angular2/core'; 
    
    import {bootstrap} from 'angular2/platform/browser';
    
    import {CORE_DIRECTIVES, FORM_DIRECTIVES, NgClass, NgIf  NgForm, Control, ControlGroup, FormBuilder, Validators} from 'angular2/common';
    
    import {RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS, Router, LocationStrategy, HashLocationStrategy} from 'angular2/router';
    
    import {Http, HTTP_PROVIDERS, RequestOptions, Headers, Request, RequestMethod} from 'angular2/http'
    

    update -

    @View is no more in angular2 now, so no need to import

    import {view} from 'angular2/core'
    

    Update 2

    As of angular2 is in RC so there is breaking change in all imports here is the list if all updated imports -

    angular2/core -> @angular/core
    angular2/compiler -> @angular/compiler
    angular2/common -> @angular/common
    angular2/platform/common -> @angular/common
    angular2/common_dom -> @angular/common
    angular2/platform/browser -> @angular/platform-browser-dynamic
    angular2/platform/server -> @angular/platform-server
    angular2/testing -> @angular/core/testing
    angular2/upgrade -> @angular/upgrade
    angular2/http -> @angular/http
    angular2/router -> @angular/router
    angular2/platform/testing/browser -> @angular/platform-browser-dynamic/testing
    
    0 讨论(0)
  • 2020-11-30 07:26

    The best way to kick of angular2 project is using Angular CLI.

    The Angular CLI makes it easy to create an application that already works, right out of the box. It already follows our best practices!

    please check this for more details.

    0 讨论(0)
提交回复
热议问题