【Angular18】封装自定义组件

1. 准备组件

2. 创建打包文件夹及部分配置文件

  1. 创建 文件夹app-legalentities-root
  2. 拷贝组件源文件到新的文件夹app-legalentities中
  3. 创建文件 .npmrc
javascript 复制代码
registry=发布地址
                        
always-auth=true
  1. 创建文件 ng-package.json
js 复制代码
{
    "$schema": "./node_modules/ng-packagr/ng-package.schema.json",
    "lib": {
      "entryFile": "public_api.ts"
    }
  }
  1. 创建文件 package.json
js 复制代码
{
  "name": "xxx-xx-test",
  "version": "0.0.9",
  "scripts": {
    "ng": "ng",
    "test": "ng test",
    "packagr": "ng-packagr -p ng-package.json"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^18.0.0",
    "@angular/common": "^18.0.0",
    "@angular/compiler": "^18.0.0",
    "@angular/core": "^18.0.0",
    "@angular/router": "^18.0.0",
    "ng-zorro-antd": "^18.0.1",
    "raise-ssh-test": "file:",
    "rxjs": "~7.8.0",
    "tslib": "^2.3.0",
    "zone.js": "~0.14.3"
  },
  "devDependencies": {
    "@angular/animations": "^18.0.0",
    "@angular/common": "^18.0.0",
    "@angular/compiler": "^18.0.0",
    "@angular/core": "^18.0.0",
    "@angular/router": "^18.0.0",
    "rxjs": "~7.8.0",
    "tslib": "^2.3.0",
    "zone.js": "~0.14.3"
  }
}
  • 创建文件 public_api.ts
js 复制代码
export * from './app-legalentities/app-legalentities.component';
export * from './app-legalentities/app-legalentities.service';

3. 修改配置文件

  • 修改 public_api.ts,以下内容为导出的模块、service、modal等
javascript 复制代码
export * from './app-legalentities/app-legalentities.component';
export * from './app-legalentities/app-legalentities.service';
  • 修改 package.json
javascript 复制代码
  "name": "app-legalentities-root", // 当前组件打包文件夹名称
  "version": "0.0.1", // 当前组件版本,每次发布后,下次发布前版本号+1
  "scripts": {
  ...
  },

 "dependencies": {
    "@angular/animations": "^18.0.0", // 公共
    "@angular/common": "^18.0.0", //公共
    "@angular/compiler": "^18.0.0", //公共
    "@angular/core": "^18.0.0", //公共
    "@angular/router": "^18.0.0", //公共
    
    "ng-zorro-antd": "^18.0.1", // 当前组件依赖包
    
    "rxjs": "~7.8.0", //公共
    "tslib": "^2.3.0", //公共
    "zone.js": "~0.14.3" //公共
  },

4. down依赖包和安装打包指令

  1. 使用npm i 安装包
  2. 执行 npm i ng-packagr //安装打包命令
javascript 复制代码
npm i ng-packagr
  1. 配置 package.json 文件,删除 dependencies 和 devDependencies中的内容
javascript 复制代码
{
  "name": "app-legalentities-root", `// 修改打包的名字`
  "version": "0.0.2",  `// 版本号 +1`
  "scripts": {
    "ng": "ng",
    "test": "ng test",
    "packagr": "ng-packagr -p ng-package.json"  `//配置打包命令`
  },
  "private": false // 这个要修改为false,下面的图忘记改了
}

5. 打包开始-生成dist文件

  1. 执行 npm run packagr 生成dist文件
javascript 复制代码
npm run packagr
  1. 进入dist 文件目录-执行 `npm pack 生成一个压缩文件
javascript 复制代码
npm pack

6. 安装vsts-npm-auth和配置

  1. 在app-legalentities-root文件夹下 安装 vsts-npm-auth
javascript 复制代码
npm install -g vsts-npm-auth --registry https://registry.npmjs.com --always-auth false
  1. 在dist根目录下创建文件.npmrc,内容同2.3
  2. 在dist文件夹中配置vsts-npm-auth
javascript 复制代码
vsts-npm-auth -config .npmrc

7. 发布打包文件

执行指令 npm publish

javascript 复制代码
npm publish

8. 引入自定义组件

  1. 在使用组件的根目录下,即跟package.json同级创建文件.npmrc,内容同2.3

  2. 安装组件 npm i app-country-select@0.0.1

javascript 复制代码
npm i app-country-select@0.0.1

遇到的问题

  1. 'npm run packagr' 打包报错了
    解决方案:将 angular框架的 app-legalentities-root文件夹移到单独的位置,首先删除已经安装好的依赖包,然后执行第四步
相关推荐
feixing_fx22 分钟前
告别枯燥字体:Web 字体加载策略与 font-display 最佳实践
前端·css·前端框架·交互·css3
Liora_Yvonne42 分钟前
不懂后端,只会 TypeScript,想独立做完整项目?这套全栈底座就是给前端准备的
前端·后端·全栈
前端Hardy44 分钟前
GitHub 爆火!236K+ Star!一套 Skills 让 AI 按工程师方式写代码
前端·后端
烬羽1 小时前
AI Coding 全流程实战:从需求到上线,我用 AI 开发了一个 NPM 包
前端·react.js·ai编程
前进的程序员1 小时前
Codex破局:前端组件秒级生成|提速React/Vue开发,可复用提示词+实战调试经验
前端·vue.js·react.js·codex
前端snow1 小时前
ai agent ---output汇总
前端
4311媒体网1 小时前
帝国CMS网站自定义搭建技巧
服务器·开发语言·前端
JunjunZ1 小时前
Vue 3 文件预览的通用设计与实现
前端·vue.js
CappuccinoRose1 小时前
ES6+ 核心语法糖
前端·es6·解构·可选链·空值合并
两只羊ovo1 小时前
Promise.all 并发请求实战:从串行 await 到双段并行
前端