首页 > 解决方案 > Angular 实践与英雄之旅和 AppRoutingModule 中的错误

问题描述

我用 Angular Tour of Heroes练习。一切都很好,直到app-routing.module部分。按照说明添加和编辑 app-routing.module.ts 后出现错误。即使我从最终代码审查中复制并粘贴所有代码, 仍然会出现相同的错误。

Error: ./node_modules/@angular/router/__ivy_ngcc__/fesm2015/router.js 43:12
Module parse failed: Identifier 'ɵngcc0' has already been declared (43:12)
File was processed with these loaders:
 * ./node_modules/@angular-devkit/build-angular/src/babel/webpack-loader.js
 * ./node_modules/@ngtools/webpack/src/ivy/index.js
You may need an additional loader to handle the result of these loaders.
| import * as ɵngcc0 from '@angular/core';
| import * as ɵngcc1 from '@angular/common';
> import * as ɵngcc0 from '@angular/core';
| import * as ɵngcc1 from '@angular/common';
| class RouterEvent {

包.json

{
  "name": "angular-tour-of-heroes",
  "version": "0.0.0",
  "private": true,
  "dependencies": {
    "@angular/animations": "~11.2.4",
    "@angular/common": "~11.2.4",
    "@angular/compiler": "~11.2.4",
    "@angular/core": "~11.2.4",
    "@angular/forms": "^11.2.4",
    "@angular/platform-browser": "~11.2.4",
    "@angular/platform-browser-dynamic": "~11.2.4",
    "@angular/router": "~11.2.4",
    "rxjs": "~6.6.0",
    "tslib": "^2.0.0",
    "zone.js": "~0.11.3"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.1102.3",
    "@angular/cli": "~11.2.3",
    "@angular/compiler-cli": "~11.2.4",
    "@types/jasmine": "~3.6.0",
    "@types/node": "^12.11.1",
    "codelyzer": "^6.0.0",
    "jasmine-core": "~3.6.0",
    "jasmine-spec-reporter": "~5.0.0",
    "karma": "~6.1.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage": "~2.0.3",
    "karma-jasmine": "~4.0.0",
    "karma-jasmine-html-reporter": "^1.5.0",
    "protractor": "~7.0.0",
    "ts-node": "~8.3.0",
    "tslint": "~6.1.0",
    "typescript": "~4.1.5"
  }
}

标签: angular

解决方案


I have a workaround, It's not the proper solution but it works for me. Solution is to open file node_modules@angular\router_ivy_ngcc_\fesm2015\router.js

enter image description here

import * as ɵngcc0 from '@angular/core';
import * as ɵngcc1 from '@angular/common';
// import * as ɵngcc0 from '@angular/core';
// import * as ɵngcc1 from '@angular/common';

comment above two lines that are repeated. for me, it's lines 43 and 44. And restart your Angular Application. It will work!


推荐阅读