Str*_*der 5 npm angular-module angular-cli angular ng-packagr
我正在尝试将本地开发的Angular项目/模块导入到angular应用程序中,而不将其发布到npm存储库中。
首先,我按照本教程以UMD格式构建模块(我跳过了发布部分):
https://medium.com/@cyrilletuzi/how-to-build-and-publish-an-angular-module-7ad19c0b4464
然后,我尝试通过执行以下命令行在最终应用程序中安装模块:
npm install ../path-to-my-module --save
Run Code Online (Sandbox Code Playgroud)
这成功添加我的模块,@myscope/myModule在package.json我的应用程序,但问题是,在应用程序模块的进口无法识别。我最终收到以下错误:
Cannot find module @myscope/myModule
Run Code Online (Sandbox Code Playgroud)
在my中node_modules,@myscope创建了文件夹,并且在其中有一个../path-to-my-module名为的快捷方式myModule
问题的根源是否在于捷径?如果可以的话如何解决?
我发现这篇文章可以帮助我解决问题:
简要概述一下,这是我的工作方式:
npm install -g ng-packagrnpm install ng-packagr --save-devng-package.json in the root folder of the project, and add the following:
{
"$schema": "./node_modules/ng-packagr/ng-package.schema.json",
"lib": {
"entryFile": "public_api.ts"
"externals": {
"@angular/cdk": "ng.cdk",
"@angular/cdk/accordion": "ng.cdk.accordion",
//...
}
}
}Run Code Online (Sandbox Code Playgroud)
In my case I had to add external dependencies in order to avoid packaging/build errors:
create public_api.ts in the root folder of the project, and add the following:
export * from './src/app/modules/myFeature/my-feature.module'
Edit package.json, and add the packagr line to the scripts tag:
"scripts": {
//...
"packagr": "ng-packagr -p ng-package.json"
}Run Code Online (Sandbox Code Playgroud)
Create the package by running the following command from the root folder:
npm run packagr
Install it for local development:
dist folder:npm packnpm install ../some-relative-path/dist/my-component-library-0.0.0.tgzThen I could import my module from any other module or component of my final project
| 归档时间: |
|
| 查看次数: |
11215 次 |
| 最近记录: |