开发Vue组件库

使用脚手架新建vue项目(以button 组件为例)
1.新建package 文件夹,添加index.js 、custombutton 文件夹,custombutton 中 添加
custombutton .vue
index.js

<template>
  <div>
    button
  </div>
</template>

<script>
export default {
  name: 'custombutton '
}
</script>

<style>
</style>
import custombutton from './custombutton .vue';
// 此处为按需加载使用
canvasBroad.install = Vue => Vue.component(canvasBroad.name, canvasBroad);//注册组件

export default custombutton 

packages/index.js

import custombutton from './custombutton/index ' 

// 存储组件列表
const components = [
    custombutton
]

// 定义 install 方法,接收 Vue 作为参数。如果使用 use 注册插件,则所有的组件都将被注册
const install = function (Vue) {
// 判断是否安装
if (install.installed) return
    // 遍历注册全局组件
    components.map(component => Vue.component(component.name, component))
}

// 判断是否是直接引入文件
if (typeof window !== 'undefined' && window.Vue) {
    install(window.Vue)
}

export default {
    // 导出的对象必须具有 install,才能被 Vue.use() 方法安装
    install,
    // 以下是具体的组件列表
    ...components
}

在package.json 添加

"lib": "vue-cli-service build --target lib --name custom --dest lib packages/index.js"
  • 按需加载需要引入插件
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值