简体   繁体   English

角度4到角度5无法找到模块'@ angular / router'

[英]angular 4 to angular 5 Cannot find module '@angular/router'

  • I am upgrading from angular 2 to angular 5. 我从角度2升级到角度5。
  • after upgradation I am seeing the below errors in the the ts file. 升级后,我在ts文件中看到以下错误。
  • in angular5 do I need to remove @angular/core and @angular/router 在angular5中我需要删除@ angular / core和@ angular / router
  • after upgradation providing the package.json also 升级后提供package.json也
  • providing my error and code snippet below. 在下面提供我的错误和代码段。
  • can you tell me how to fix it. 你能告诉我如何解决它吗?

error 错误

[ts] Cannot find module '@angular/core'. [ts]无法找到模块'@ angular / core'。
[ts] Cannot find module '@angular/router'. [ts]无法找到模块'@ angular / router'。

code

import {Component, ElementRef, Input,Output,EventEmitter,Inject, OnInit,ViewChild} from '@angular/core';
import {KendoGridComponent} from '../grid/grid.component'
import { Router} from '@angular/router';

package.json 的package.json

{
  "name": "SPORTS",
  "version": "1.0.0",
  "description": "SPORTS player - v0.2.37.1",
  "engines": {
    "node": "7.10.1"
  },
  "license": "ISC",
  "dependencies": {
    "@angular/animations": "^5.2.2",
    "@angular/common": "^5.2.2",
    "@angular/compiler": "^5.2.2",
    "@angular/compiler-cli": "^5.2.2",
    "@angular/core": "^5.2.2",
    "@angular/forms": "^5.2.2",
    "@angular/http": "^5.2.2",
    "@angular/platform-browser": "^5.2.2",
    "@angular/platform-browser-dynamic": "^5.2.2",
    "@angular/platform-server": "^5.2.2",
    "@angular/router": "^5.2.2",
    "@angular/upgrade": "2.0.0",
    "@ng-idle/core": "2.0.0-beta.2",
    "@ng-idle/keepalive": "2.0.0-beta.2",
    "@types/jquery": "^3.2.10",
    "@types/node": "^8.0.18",
    "@types/underscore": "^1.8.1",
    "@types/webpack": "^3.0.5",
    "angular2-in-memory-web-api": "0.0.21",
    "angular2-router-loader": "^0.3.5",
    "angular2-template-loader": "^0.6.2",
    "awesome-typescript-loader": "^3.2.1",
    "babel-minify-webpack-plugin": "0.2.0",
    "bootstrap": "^3.3.6",
    "commander": "^2.6.0",
    "compression-webpack-plugin": "1.0.1",
    "core-js": "^2.4.1",
    "css-loader": "^0.28.7",
    "del": "^3.0.0",
    "express": "^4.14.1",
    "extract-text-webpack-plugin": "^3.0.0",
    "file-loader": "^0.11.2",
    "file-saver": "^1.3.3",
    "html-webpack-plugin": "^2.30.1",
    "http-post": "^0.1.1",
    "http-proxy": "^1.8.1",
    "jquery": "2.2.3",
    "pako": "^1.0.5",
    "path": "^0.12.7",
    "raw-loader": "^0.5.1",
    "reflect-metadata": "^0.1.3",
    "require": "^2.4.20",
    "rxjs": "^5.5.6",
    "style-loader": "^0.18.2",
    "text-encoding-shim": "^1.0.0",
    "to-string-loader": "^1.1.5",
    "ts": "0.0.0",
    "typescript": "^2.4.2",
    "uglifyjs-webpack-plugin": "^0.4.6",
    "underscore": "^1.8.3",
    "webpack": "^3.5.6",
    "webpack-dev-server": "^2.7.1",
    "webpack-merge": "^4.1.0",
    "zone.js": "^0.6.23"
  },
  "devDependencies": {
    "gulp": "^3.9.1",
    "gulp-clean-css": "^3.8.0",
    "gulp-concat": "^2.6.1",
    "gulp-util": "^3.0.8",
    "jasmine-core": "~2.4.1",
    "jasmine-expect": "^3.0.1",
    "jasmine-jquery": "^2.1.1",
    "karma": "^1.2.0",
    "karma-chrome-launcher": "^0.2.3",
    "karma-cli": "^0.1.2",
    "karma-coverage": "^1.1.1",
    "karma-htmlfile-reporter": "^0.2.2",
    "karma-jasmine": "^0.3.8",
    "karma-jquery": "^0.1.0",
    "karma-sourcemap-loader": "^0.3.7",
    "karma-typescript": "^2.1.4",
    "karma-typescript-preprocessor": "^0.3.0"
  },
  "main": "server.js",
  "scripts": {
    "dev": "webpack-dev-server --config  ./webpack.dev.js --inline --progress --color ",
    "postinstall": "webpack -p --config ./webpack.prod.js --progress --profile --bail",
    "start": "node server.js"
  },
  "repository": {
    "type": "git",
    "url": "https://github.iSPORTS.com/player-Replatform/webcomponent.git"
  }
}

You need to install these dependencies manually. 您需要手动安装这些依赖项。

$ npm i @angular/core -s
$ npm i @angular/router -s

I also faced the same issue while upgrading because of npm . 由于npm我在升级时也遇到了同样的问题。 I suggest you start using yarn instead. 我建议你开始使用纱线代替。

  1. Is this error coming from the compiler or an IDE tool like Intellisense? 这个错误是来自编译器还是像Intellisense这样的IDE工具? I find sometimes with installing/branch switching IDE tools like this can get confused and require a restart of the IDE. 我发现有时安装/分支切换这样的IDE工具可能会混淆并需要重新启动IDE。

  2. If it won't compile, are the modules actually in your node_modules? 如果它不能编译,那么模块实际上是在node_modules? If not, delete your package-lock.json or shrinkwrap.json and install again. 如果没有,删除package-lock.jsonshrinkwrap.json并重新安装。 NPM adds the package lock by default as of 5.0 and I find with complicated dependencies it can mess up sometimes. 默认情况下,NPM从5.0开始添加包锁,我发现它有复杂的依赖关系,有时会搞乱。

  3. If they are there, run npm prune and npm install again. 如果他们在那里,再次运行npm prunenpm install

  4. If that doesn't work, nuke node_modules alongside any lock file and install again. 如果这不起作用, node_modules nuke node_modules与任何锁定文件一起再次安装。

  5. Do any third-party libraries require an update? 是否有任何第三方库需要更新? Angular 5 had some breaking API changes. Angular 5有一些破坏API的变化。 Check their docs if that's the case. 检查他们的文档是否属实。

  6. Consider Yarn. 考虑纱线。

I'm pretty sure it's the first one, but I'll leave all the steps I usually take just for the sake of completion. 我很确定这是第一个,但为了完成,我会留下我通常采取的所有步骤。

Make sure you are exporting RouterModule in each router module that has children. 确保在每个有子节点的路由器模块中导出RouterModule In the angular documentation this is not explicitly stated as being necessary -- it's merely included in the examples -- but it is necessary, as one finds out after the customary several hours of edit-run-sigh, edit-run-shout, edit-run-google-editWithHope-scream-considerSmashingKeyboard, edit-run-cry, edit-run-solveByAccident-feelMomentaryPride (repeat). 在角度文档中,这没有明确说明是必要的 - 它仅仅包含在示例中 - 但是有必要,正如人们在习惯性的几个小时的编辑 - 运行 - 叹息,编辑 - 运行 - 呼喊之后发现的那样,编辑-run-google-editWithHope-scream-considerationSmashingKeyboard,edit-run-cry,edit-run-solveByAccident-feelMomentaryPride(repeat)。

The upshot: include RouterModule . 结果:包括RouterModule The meta upshot: pay attention to the little seemingly unnecessary code in official documentation. 元结果:注意官方文档中看似不必要的小代码。

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

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