繁体   English   中英

找不到模块'angular2 / angular2'

[英]Cannot find module 'angular2/angular2'

我正在开发一个带有angular2和gulp的节点应用程序。 我已经编写了一个组件文件login.ts,如下所示:

import {Component, View} from 'angular2/angular2';
import {FormBuilder,  formDirectives } from 'angular2/forms';
@Component({
  selector: 'login',
  injectables: [FormBuilder]
})
@View({
  templateUrl: '/scripts/src/components/login/login.html',
  directives: [formDirectives]
})

export class login {

}

我的bootstrap.ts文件是:

import {bootstrap} from 'angular2/angular2';

import {login} from './components/login/login';

bootstrap(login);

但是当我编译这些文件时,它会给我以下错误:

client\bootstrap.ts(1,25): error TS2307: Cannot find module 'angular2/angular2'.
client\components\login\login.ts(1,31): error TS2307: Cannot find module 'angular2/angular2
client\components\login\login.ts(2,45): error TS2307: Cannot find module 'angular2/forms'.

这是我的tsconfig.json:

{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "sourceMap": true,
        "watch": true,
        "removeComments": true,
        "moduleResolution": "node",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true
    }
}

我已经安装了angular2的输入:

tsd install angular2 --save

请帮助修复错误。

@simon错误说是在进口中。 但对于进一步的进口,我发布了这个答案可能对其他人也有用。

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'

更新 -

@View现在不再是angular2,所以不需要导入

import {view} from 'angular2/core'

更新2

由于angular2在RC中,所以所有进口都有重大变化,如果所有更新的进口都是列表 -

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

它在测试之前就改变了模块

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

仅供参考:bootstrap也改为

import {bootstrap} from 'angular2/platform/browser';

因此,网上的很多博客文章都已过时:-(

根据Angular2 rc1的新版本,您可以将package.json更新为

"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",
    .....
 }

除此之外,还可以在组件或容器中导入以下内容:

import { Component } from '@angular/core';
import {ROUTER_DIRECTIVES, Router, RouteParams} from '@angular/router-deprecated';

请注意,作为Angular2最终版本的形式,正确答案是这样的。

import {Component, View, Directive, Input, Output, Inject, Injectable, provide} from 'angular/core'; 

import {bootstrap} from 'angular/platform/browser';

import {CORE_DIRECTIVES, FORM_DIRECTIVES, NgClass, NgIf  NgForm, Control, ControlGroup, FormBuilder, Validators} from 'angular/common';

import {RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS, Router, LocationStrategy, HashLocationStrategy} from 'angular/router';

import {Http, HTTP_PROVIDERS, RequestOptions, Headers, Request, RequestMethod} from 'angular/http'

如上面的yups update 2中所述,这是正确的

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

您正在尝试从node_modules的错误/旧目录访问Component。 请从中访问组件

import { Component, OnInit } from '@angular/core';

而不是: import { Component, View } from 'angular2/angular2';

从波纹管路径访问bootstrap:

import { bootstrap } from 'angular2/platform/browser';

你必须在最终版本中更新Angular2版本 -

然后使用像----

import { Component } from '@angular/core';

有一个像'@angular/core'这样的更新库

从''angular2 / angular2'导入的是之前的版本,现在不再支持了。现在我们必须从'angular2 / core'导入相同的内容。有关详细参考用途( https://angular.io/guide/quickstart

'angular2 / angular2'不是angular2的有效依赖项

你必须首先检查package.json文件“@ angular / core”是否存在

"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",
    .....
 }

看到像这样的组件文件,你也使用formGroup作为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]
        });
    }

而不是必须在app.module.ts文件中导入ReactiveFormsModule
从'@ angular / forms'导入{ReactiveFormsModule};

没有ReactiveFormsModule formGroup不起作用会产生错误

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]
})

启动angular2项目的最佳方法是使用Angular CLI

Angular CLI可以轻松创建已经可以正常工作的应用程序。 它已经遵循我们的最佳实践!

查看此更多详细信息。

import {Component} from "@angular/core";

@Component({
  selector: "userForm",
  template: '<div><input type="text" name="name" [disabled]="flag"/></div>'
});

export class userForm{
 public flag = false; //boolean value: true/false
}
  • 首先在packege.json更新angular2 liberies。
  • 第二

      import {Component} from "@angular/core"; import {FormBuilder } from "@angular/forms"; @Component({ selector: "login", providers: [FormBuilder], templateUrl: "/scripts/src/components/login/login.html" }) export class login { } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM