找不到模块“@angular/forms”

问题描述 投票:0回答:6

我正在开发一个 Ionic 2/Angular 应用程序,并尝试按照本教程使用表单验证: https://angular.io/docs/ts/latest/cookbook/dynamic-form.html

但是编译器说:

找不到模块“@angular/forms”。

为什么这个模块不可用?它已被弃用吗?

forms validation angular ionic2
6个回答
12
投票

尝试

npm install @angular/forms --save

它会警告您应该使用 2.0.0-rc.4。这可能会带来它自己的一系列升级挑战......


10
投票

我遇到了同样的问题,我的解决方案是:

1)将表单添加到package.json:

"dependencies": {
...
"@angular/forms":   "0.2.0",
...
}

2)在控制台中使用 npm 安装,在应用程序文件夹中输入

npm install

3)再次运行应用程序

npm start

希望这有帮助


3
投票

运行此命令

npm install @angular/forms --save

转到 package.json 并仔细检查以确保存在此依赖项

@angular/forms: "{your version}",
并保存更改。

关闭并重新打开 IDE,然后返回到 app.module.ts。删除导入语句并重新输入,然后它应该可以工作。

谢谢你


0
投票

有时更新版本时我会看到这样的消息:

npm ERR! errno -4048
npm ERR! syscall rename
npm ERR! Error: EPERM: operation not permitted, rename 'R:\TFS\RRCRM\RRCRM\node_modules\@angular\forms' -> 'R:\TFS\RRCRM\RRCRM\node_modules\@angular\.forms.DELETE'
npm ERR!     at Error (native)
npm ERR!  { [Error: EPERM: operation not permitted, rename 'R:\TFS\RRCRM\RRCRM\node_modules\@angular\forms' -> 'R:\TFS\RRCRM\RRCRM\node_modules\@angular\.forms.DELETE']
npm ERR!   errno: -4048,
npm ERR!   code: 'EPERM',
npm ERR!   syscall: 'rename',
npm ERR!   path: 'R:\\TFS\\RRCRM\\RRCRM\\node_modules\\@angular\\forms',
npm ERR!   dest: 'R:\\TFS\\RRCRM\\RRCRM\\node_modules\\@angular\\.forms.DELETE',
npm ERR!   parent: 'rrcrm' }
npm ERR! 
npm ERR! Please try running this command again as root/Administrator.
npm ERR! Please include the following file with any support request:
npm ERR!     R:\TFS\RRCRM\RRCRM\npm-debug.log

我相信这有时可能是由于文件使用错误造成的(我在 Windows 上以管理员身份运行 Visual Studio)。

确保没有运行任何可能对任何包有文件锁定的内容 - 例如 Angular CLI。因此,请关闭所有窗口或进程,然后再次运行类似

npm install
的内容。


-1
投票

我遇到了一个奇怪的情况,我无法进行 npm install,并且必须从共享路径复制库。复制node_modules/@angaular下的“forms”文件夹解决了问题。


-3
投票

import { FormsModule } from '@angular/forms'; 被注入到 app.module.ts 中,并在导入中添加 FormsModule 数组,如下所示

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


    imports: [
      BrowserModule,
      FormsModule
    ]
© www.soinside.com 2019 - 2024. All rights reserved.