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

时间:2017-05-01 10:02:40

标签: angular angular-cli

当我启动一个空白的Angular CLI项目时,来自package.json的这些依赖关系看起来没有必要,所以我试图删除它们(以及从导入中删除FormModules和HttpModules):

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

但是当我尝试构建项目时,我收到了错误:

' ERROR in无法解析模块@ angular / router'

对我来说更奇怪的是,在重新保存文件后,项目重建成功并且有效。

有人可以从@ angular / router解释这个隐藏的依赖吗?

我的档案:

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:

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

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

1 个答案:

答案 0 :(得分:4)

Angular cli使用@ngtool/webpack插件,该插件使用来自@angular/compiler-cli的私有API来获取延迟加载的路由

<强> 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

// 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

注意this.firstRun。这就是你在第一次运行时收到错误的原因。

<强> @角/编译-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

这是复制

enter image description here

另见