首页 > 解决方案 > 将路由器添加到 Angular 9 项目

问题描述

我正在尝试将路由器添加到创建时没有对路由器说是的项目中。这是我正在运行的命令:

npm install -g @angular/router

这里是项目配置和命令的结果:

Angular CLI: 9.1.1
Node: 10.15.1
OS: win32 x64

Angular: 9.1.2
... common, compiler, compiler-cli, core
Ivy Workspace: Yes

Package                             Version
-------------------------------------------------------------
@angular-devkit/architect           0.901.1
@angular-devkit/build-angular       0.901.1
@angular-devkit/build-ng-packagr    0.901.1
@angular-devkit/build-optimizer     0.901.1
@angular-devkit/build-webpack       0.901.1
@angular-devkit/core                9.1.1
@angular-devkit/schematics          9.1.1
@angular/animations                 9.0.7
@angular/cdk                        9.2.1
@angular/cli                        9.1.1
@angular/flex-layout                9.0.0-beta.29
@angular/forms                      9.0.7
@angular/language-service           9.0.7
@angular/material                   9.2.1
@angular/platform-browser           9.0.7
@angular/platform-browser-dynamic   9.0.7
@angular/router                     9.0.7
@ngtools/webpack                    9.1.1
@schematics/angular                 9.1.1
@schematics/update                  0.901.1
ng-packagr                          9.1.1
rxjs                                6.5.5
typescript                          3.7.5
webpack                             4.42.0

PS D:\PrismNewUI\PrismWebUIUX> npm install -g @angular/router
npm WARN @angular/router@9.1.3 requires a peer of @angular/core@9.1.3 but none is installed. You must install peer dependencies yourself.
npm WARN @angular/router@9.1.3 requires a peer of @angular/common@9.1.3 but none is installed. You must install peer dependencies yourself.
npm WARN @angular/router@9.1.3 requires a peer of @angular/platform-browser@9.1.3 but none is installed. You must install peer dependencies yourself.
npm WARN @angular/router@9.1.3 requires a peer of rxjs@^6.5.3 but none is installed. You must install peer dependencies yourself.
npm WARN @angular/router@9.1.3 requires a peer of tslib@^1.10.0 but none is installed. You must install peer dependencies yourself.

+ @angular/router@9.1.3
updated 1 package in 0.482s

在我看来,当前的路由器版本高于我的项目。

据我了解,路由器没有安装。在这种情况下我该怎么办?

谢谢

没有全局标志:

PS D:\PrismNewUI\PrismWebUIUX> npm install @angular/router
npm WARN @angular-devkit/build-ng-packagr@0.901.1 requires a peer of tsickle@~0.37.1 || ~0.38.0 but none is installed. You must install peer dependencies yourself.
npm WARN @angular/animations@9.0.7 requires a peer of @angular/core@9.0.7 but none is installed. You must install peer dependencies yourself.
npm WARN @angular/forms@9.0.7 requires a peer of @angular/core@9.0.7 but none is installed. You must install peer dependencies yourself.
npm WARN @angular/forms@9.0.7 requires a peer of @angular/common@9.0.7 but none is installed. You must install peer dependencies yourself.
npm WARN @angular/platform-browser@9.0.7 requires a peer of @angular/core@9.0.7 but none is installed. You must install peer dependencies yourself.
npm WARN @angular/platform-browser@9.0.7 requires a peer of @angular/common@9.0.7 but none is installed. You must install peer dependencies yourself.
npm WARN @angular/platform-browser-dynamic@9.0.7 requires a peer of @angular/core@9.0.7 but none is installed. You must install peer dependencies yourself.
npm WARN @angular/platform-browser-dynamic@9.0.7 requires a peer of @angular/common@9.0.7 but none is installed. You must install peer dependencies yourself.
npm WARN @angular/platform-browser-dynamic@9.0.7 requires a peer of @angular/compiler@9.0.7 but none is installed. You must install peer dependencies yourself.
npm WARN @rollup/plugin-json@4.0.2 requires a peer of rollup@^1.20.0 but none is installed. You must install peer dependencies yourself.
npm WARN sass-loader@8.0.2 requires a peer of node-sass@^4.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN sass-loader@8.0.2 requires a peer of fibers@>= 3.1.0 but none is installed. You must install peer dependencies yourself.
npm WARN webpack-subresource-integrity@1.4.0 requires a peer of html-webpack-plugin@^2.21.0 || ~3 || >=4.0.0-alpha.2 <5 but none is installed. You must install peer dependencies yourself.
npm WARN @angular/router@9.0.7 requires a peer of @angular/core@9.0.7 but none is installed. You must install peer dependencies yourself.
npm WARN @angular/router@9.0.7 requires a peer of @angular/common@9.0.7 but none is installed. You must install peer dependencies yourself.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.1.2 (node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.1.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.12 (node_modules\webpack-dev-server\node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.12: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.12 (node_modules\watchpack\node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.12: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

+ @angular/router@9.0.7
updated 1 package and audited 17382 packages in 11.929s
found 2 low severity vulnerabilities
  run `npm audit fix` to fix them, or `npm audit` for details

标签: angular-cliangular9

解决方案


npm install -g @angular/router在您的计算机上全局安装路由器。基本上在 Windows 上,全局安装的 npm 依赖项可以在%AppData%/npm路径上找到。如果全局安装的 npm 包具有命令行界面(如angular-cli),则可以直接从 shell 访问命令。

如果您想在本地项目中安装路由器,您可以将所需的版本“手动”添加到package.jsonJSONdependencies条目中,并从可以找到npm install的路径中调用。package.json新添加的依赖将下载node_modules到项目的.

或者,如果您不想“手动”将其添加到package.json您可以调用项目npm install @angular/router@<wishedversion>所在的路径package.json。这样路由器将被安装在node_modules并且package.json将被更新。(当然<wishedversion>应该是您需要的版本。)

但是我在依赖项中看到了@angular/router: 9.0.7. 这意味着,它已经在,package.json所以只需npm install要从路径中调用package.json来将它安装在本地node_modules


推荐阅读