angular4发布到npm仓库
介绍通过 实现工程创建ng new yaya-npm-publish-demo cd yaya-npm-publish-demo/src/app ## 创建模块 ng g m yaya-module cd yaya-module ## 创建组件 ng g c yaya-component ## 创建普通类 echo "" >>data.ts data.ts export class Data { name: string; value: string; }
此时目录结构 src ├── app │ ├── app.component.css │ ├── app.component.html │ ├── app.component.spec.ts │ ├── app.component.ts │ ├── app.module.ts │ └── yaya-module │ ├── data.ts │ ├── yaya-component │ │ ├── yaya-component.component.css │ │ ├── yaya-component.component.html │ │ ├── yaya-component.component.spec.ts │ │ └── yaya-component.component.ts │ └── yaya-module.module.ts ng-packagr在项目根目录执行以下命令安装 npm install ng-packagr --save-dev 在项目根目录下创建 ng-package.json { "$schema": "./node_modules/ng-packagr/ng-package.schema.json","whitelistedNonPeerDependencies": [ "." ],"lib": { "entryFile": "public-api.ts" } } public-api.ts /** * 导出module模块 */ export * from './src/app/yaya-module/yaya-module.module' /** * 导出普通类 */ export * from './src/app/yaya-module/data' /**如果有其他需要导出的对象都可以写在public-api.ts里**/ 修改 package.json { "name": "yaya","version": "0.0.0","license": "MIT","scripts": { "ng": "ng","start": "ng serve","build": "ng build --prod","test": "ng test","lint": "ng lint","e2e": "ng e2e","package": "ng-packagr -p ng-package.json" },"private": false ... } npm publish在根目录下执行命令 ## 设置仓库地址为npm官方仓库地址(国内大部分都使用阿里仓库地址如果没改publish会失败) npm config set registry https://registry.npmjs.org/ ## 登陆npm,用户名密码邮箱需要全部匹配 npm login Username: xxxxx Password: Email: (this IS public) jianfeng.zheng@definesys.com Logged in as xxxxx on https://registry.npmjs.org/. # 登陆完就可以publish cd dist npm publish ##输出以下信息说明上传成功 npm notice === Tarball Details === npm notice name: yaya-npm-publish-demo npm notice version: 0.0.0 npm notice package size: 7.6 kB npm notice unpacked size: 34.4 kB npm notice shasum: 7ce18c7a0aedaaf902600081a7dffbeb8a17e874 npm notice integrity: sha512-LjW0wdRb8lYnZ[...]QsXwro5Ih7GHA== npm notice total files: 27 npm notice + yaya-npm-publish-demo@0.0.0 使用
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { DemoModule } from 'yaya'; import { AppComponent } from './app.component'; import { YayaModuleModule } from 'yaya-npm-publish-demo'; @NgModule({ declarations: [ AppComponent ],imports: [ BrowserModule,YayaModuleModule <===这里 ],providers: [],bootstrap: [AppComponent] }) export class AppModule { } 普通类可通过import命令导入,如: import{Data} from 'yaya-npm-publish-demo'; 组件和正常组件使用方法一样 <app-yaya-component></app-yaya-component> 升级升级需要增加版本号,每次都需要修改 其他问题
npm ERR! 301 Could not create user npm ERR! undefined (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |