因此,当我尝试在一个使用 npm 链接的 Angular 项目中进行开发时,我遇到了一个问题,该库还包含它自己的节点模块文件夹。两者都需要自己的节点模块副本,因为两者都将包含自己的组件,并且消费者需要导入库项目的模块。
假设我有一个链接到 LibraryProject 的 ConsumerProject。我在命令行中运行以下命令:在 LibraryProject 的根目录
npm link
ConsumerProject 的根
npm link library-project
两者都包含相同的以下依赖项/devDependencies:
“依赖”:{
"@angular/animations": "^4.2.4",
"@angular/common": "^4.2.4",
"@angular/compiler": "^4.2.4",
"@angular/core": "^4.2.4",
"@angular/forms": "^4.2.4",
"@angular/http": "^4.2.4",
"@angular/platform-browser": "^4.2.4",
"@angular/platform-browser-dynamic": "^4.2.4",
"@angular/router": "^4.2.4",
"core-js": "^2.4.1",
"rxjs": "^5.4.2",
"zone.js": "^0.8.14"
},“开发依赖”:{
"@angular/cli": "1.3.2",
"@angular/compiler-cli": "^4.2.4",
"@angular/language-service": "^4.2.4",
"@types/jasmine": "~2.5.53",
"@types/jasminewd2": "~2.0.2",
"@types/node": "~6.0.60",
"codelyzer": "~3.1.1",
"jasmine-core": "~2.6.2",
"jasmine-spec-reporter": "~4.1.0",
"karma": "~1.7.0",
"karma-chrome-launcher": "~2.1.1",
"karma-cli": "~1.0.1",
"karma-coverage-istanbul-reporter": "^1.2.1",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.1.2",
"ts-node": "~3.2.0",
"tslint": "~5.3.2",
"typescript": "~2.3.3"
}
ConsumerProject 将从 LibraryProject 导入 LibraryModule。当我在 ConsumerProject 中运行 ng serve 时,出现以下错误:
ERROR in Error encountered resolving symbol values statically. Function
calls are not supported. Consider replacing the function or lambda with a
reference to an exported function (position 194:50 in the original .ts
file), resolving symbol NgModule in *path to directory*, resolving symbol
LibraryModule in *path to directory*, resolving symbol LibraryModule in
*path to directory*
重新编译代码将导致代码编译,当我加载页面时,我收到以下错误:未捕获错误:模块“AppModule”导入的意外值“LibraryModule”。请添加 @NgModule 注释。
我对这个问题做了一些研究,我发现的一件事是,如果安装的 angular/cli 包的版本为 1.2.7 或更低,您可以通过将此代码添加到您的 tsconfig.json 来成功运行您的项目文件:
"path": {
"@angular/*": [
"../node_modules/@angular/*"
]
}
这是假设您的 baseUrl 在您的 src 目录中。但是,在 cli 1.3.0 中使用它不再解决问题。通过简单地删除我的库中的节点模块文件夹,直到我需要使用它,我已经能够暂时绕过这个问题。但是,我希望对问题本身有某种实际的解决方案,而不必卸载并重新安装节点模块文件夹本身。