Fork me on GitHub

使用@vue/cli在npm上创建,发布Vue.js组件库

首先,从安装vue-cli开始

npm i -g @vue/cli  或  yarn global add @vue/cli

vue create my-vue-lib

npm install  或  yarn install

npm run serve

然后,开始创建一个简单的组件

复制代码
 1 <template>
 2   <div class="banner" :style="bannerStyles" :class="`banner__${position}`">
 3     <slot></slot>
 4   </div>
 5 </template>
 6 
 7 <script>
 8 const defaultStyles = {
 9   left: 0,
10   right: 0,
11 };
12 export default {
13   props: {
14     position: {
15       type: String,
16       default: 'top',
17       validator(position) {
18         return ['top', 'bottom'].indexOf(position) > -1;
19       },
20     },
21     styles: {
22       type: Object,
23       default: () => ({}),
24     },
25   },
26   data() {
27     return {
28       bannerStyles: {
29         ...defaultStyles,
30         ...this.styles,
31       },
32     };
33   },
34 };
35 </script>
36 
37 <style lang="scss" scoped>
38 .banner {
39   padding: 12px;
40   
41   color: #f6a623;
42   text-align: left;
43   position: fixed;
44   z-index: 2;
45 }
46 .banner__top {
47   top: 0;
48 }
49 .banner__bottom {
50   bottom: 0;
51 }
52 </style>
复制代码

现在,通过npm使用这个组件

第1步 - 设置库构建

在package.json中添加编辑脚本:

scripts: {
  "build-bundle": "vue-cli-service build --target lib --name zchart ./src/index.js"
},
"peerDependencies": {
  "vue": "^2.6.11"
},

npm run build-bundle 将在 dist目录下输出:

dist/zchart.umd.min.js
dist/zchart.umd.js    // 一个直接给浏览器或AMD loader使用的UMD包
dist/zchart.common.js   // 一个给打包器用的CommonJS包
dist/zchart.css       // 提取出来的CSS文件,可以通过在vue.config.js中设置 css: { extract: false } 强制内联

./src/index.js 入口文件

复制代码
import ZchartBanner from '../packages/banner/index'

const components = [ ZchartBanner ]

const install = function (Vue, opts = {}) {
  components.forEach((component) => {
    Vue.component(component.name, component)
  })

  Vue.prototype.$ZCHART = {
    requestUrl: opts.requestUrl || '', // 请求接口 url
    headers: opts.headers || {}, // 请求头
    colors: opts.colors || ['#F26A6A', '#365EEE', '#F5C358', '#4EBF7A'] // 图表色系
  }
}

/* istanbul ignore if */
if (typeof window !== 'undefined' && window.Vue) {
  install(window.Vue)
}

// 通过插件单独引入
export default {
  install,
  ZchartBanner
}
复制代码

第2步 - 指定package.json中的输出文件

"main": "./dist/zchart.common.js",

第3步 - 以用户身份在npm上添加/登陆

确保在npm上注册,npm adduser 注册一个新用户和 npm login 作为一个现有用户登陆

第4步 - 验证npm用户凭证

输入npm whoami 来验证你的用户名

第5步 - 命名你的组件库

为你的包选择一个名字,你必须确保它尚未被使用。确保把它放在你的package.json中

"name": "zchart",  // 或 "name": "@eric/zchart"

第6步 - 构建

npm run build-bundle

第7步 - 在npm上发布组件库

运行 npm publish -access public 来发布该库,以供公众访问

第8步 - 如何使用你新发布的库

从npm安装组件库,并将组件导入代码中

npm install --save zchart

现在,可以开始使用你的组件了

import Zchart from 'zchart'

Vue.use(Zchart, {
  requestUrl: window.VUE_APP_BASE_API + '/api/test/graph'
})

<zchart-banner :position="top" />

 


 

 

posted @   树山君  阅读(263)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示