简体   繁体   English

Angular CLI依赖项 - 如果我不使用它,为什么需要安装@ angular / router?

[英]Angular CLI dependencies - why do I need to install @angular/router if I don't use it?

When I start a blank Angular CLI project, these dependencies from package.json look like not necessary, so I'm trying to remove them (as well as removing FormModules and HttpModules from imports): 当我启动一个空白的Angular CLI项目时,来自package.json的这些依赖项看起来没有必要,所以我试图删除它们(以及从导入中删除FormModules和HttpModules):

@angular/forms": "^4.0.0",
@angular/http": "^4.0.0",
@angular/router": "^4.0.0",

But when I try build the project, I'm getting an error: 但是当我尝试构建项目时,我收到一个错误:

'ERROR in Could not resolve module @angular/router' 'ERROR in无法解析模块@ angular / router'

And what looks even more weird to me, after re-saving a file the project rebuilds successfully and it works. 对于我来说看起来更奇怪的是,在重新保存文件后,项目重建成功并且可以正常工作。

Can somebody explain where is this hidden dependency from @angular/router? 有人可以解释@ angular / router这个隐藏的依赖吗?

My files: 我的文件:

app.modules.ts: app.modules.ts:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';    
import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {}

app.component.ts: app.component.ts:

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

@Component({
  selector: 'app-root',
  template: '<h1>test</h1>'
})
export class AppComponent {}

Angular cli uses @ngtool/webpack plugin that uses private API from @angular/compiler-cli to get lazy loaded routes Angular cli使用@ngtool/webpack插件,它使用来自@angular/compiler-cli私有API来获取延迟加载的路由

plugin.ts plugin.ts

const {__NGTOOLS_PRIVATE_API_2} = require('@angular/compiler-cli');

https://github.com/angular/angular-cli/blob/v1.0.1/packages/%40ngtools/webpack/src/plugin.ts#L7 https://github.com/angular/angular-cli/blob/v1.0.1/packages/%40ngtools/webpack/src/plugin.ts#L7

// We need to run the `listLazyRoutes` the first time because it also navigates libraries
// and other things that we might miss using the findLazyRoutesInAst.
let discoveredLazyRoutes: LazyRouteMap = this.firstRun ?
__NGTOOLS_PRIVATE_API_2.listLazyRoutes({
  program: this._program,
  host: this._compilerHost,
  angularCompilerOptions: this._angularCompilerOptions,
  entryModule: this._entryModule
})
: this._findLazyRoutesInAst();

https://github.com/angular/angular-cli/blob/v1.0.1/packages/%40ngtools/webpack/src/plugin.ts#L492-L501 https://github.com/angular/angular-cli/blob/v1.0.1/packages/%40ngtools/webpack/src/plugin.ts#L492-L501

Notice this.firstRun .That's why you are getting the error on the first run. 注意this.firstRun 。这就是你在第一次运行时收到错误的原因。

@angular/compiler-cli/src/ngtools_impl.ts @角/编译-CLI / SRC / ngtools_impl.ts

const ROUTER_MODULE_PATH = '@angular/router';

https://github.com/angular/angular/blob/4.1.0/packages/compiler-cli/src/ngtools_impl.ts#L20 https://github.com/angular/angular/blob/4.1.0/packages/compiler-cli/src/ngtools_impl.ts#L20

Here is reproduction 这是复制品

在此输入图像描述

See also 也可以看看

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

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