如何从0撸出一个Vue组件库并发布到npm?下面本篇文章手把手带大家从0开发一个Vue组件库,看看怎么发布到npm,希望对大家有所帮助。

如何从0撸出一个Vue组件库并发布到npm-LMLPHP

1、新建文件夹在终端打开执行 npm init -y

{
  "name": "vuecomponentdi",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}
登录后复制

2、建立目录结构

-- vueComponentDi
    -- packages
        -- button
            -- index.js
            -- index.vue
        -- toast
            -- index.js
            -- index.vue
    -- index.js
    -- package.json
登录后复制

3、本地调试

  • vueComponentDi/index.js
export default function(){
    console.log('本地调试')
}
登录后复制
  • 新建一个vue项目
vue create testvue
登录后复制
"devDependencies": {
    ...
    "vuecomponentdi": "F:/vueComponent@Di/vueComponentDi",//根据自己实际项目地址填写
    ...
    }
登录后复制
  • 执行npm link
  • vuecomponentdi安装Eslint

安装方法:

 npm install eslint@6.7.2 --save-dev
 ./node_modules/.bin/eslint --init
登录后复制
  • 在testvue使用vuecomponentdi
<template>
  <div class="home">
    <img alt="Vue logo" src="../assets/logo.png">
    <HelloWorld msg="Welcome to Your Vue.js App"/>
  </div>
</template>

<script>
// @ is an alias to /src
import HelloWorld from '@/components/HelloWorld.vue'
import test from "vuecomponentdi"
export default {
  name: 'Home',
  components: {
    HelloWorld
  },
  created(){
    test()
  }
}
</script>
登录后复制

控制台打印>本地调试

4、开发一个button组件

  • button模块:进入vueComponentDi/packages/button/index.vue
<template>
    <div>
        <button class="di-button"  v-on="$listeners" :class="[type?`di-button--${type}`:'']"><slot></slot></button>
    </div>
</template>
<script>
export default {
    name:"di-button",
    props:{
        type:String
    }
}
</script>
<style>
.di-button{
    display: inline-block;
    line-height: 1;
    white-space: nowrap;
    cursor: pointer;
    background: #fff;
    border: 1px solid #dcdfe6;
    color: #606266;
    -webkit-appearance: none;
    text-align: center;
    box-sizing: border-box;
    outline: none;
    margin: 0;
    transition: .1s;
    font-weight: 500;
    -moz-user-select: none;
    -webkit-user-select: none;
    -ms-user-select: none;
    padding: 12px 20px;
    font-size: 14px;
    border-radius: 4px;
}
.di-button:focus, .di-button:hover {
    color: #409eff;
    border-color: #c6e2ff;
    background-color: #ecf5ff;
}
.di-button:active {
    color: #3a8ee6;
    border-color: #3a8ee6;
    outline: none;
}
.di-button--primary {
    color: #fff;
    background-color: #409eff;
    border-color: #409eff;
}
.di-button--primary:focus, .di-button--primary:hover {
    background: #66b1ff;
    border-color: #66b1ff;
    color: #fff;
}
.di-button--primary.is-active, .di-button--primary:active {
    background: #3a8ee6;
    border-color: #3a8ee6;
    color: #fff;
}
</style>
登录后复制
  • button模块导出:进入vueComponentDi/packages/button/index.js
import button from "./index.vue"
button.install=(Vue)=>{
    Vue.component(button.name,button)
}
export default button
登录后复制
  • 聚合导出button:进入vueComponentDi/index.js
import diButton from "./packages/button"
export {
    diButton
}
登录后复制
  • 在testvue使用
<template>
  <div class="home">
    <di-button type="primary">按钮</di-button>
  </div>
</template>
<script>
// @ is an alias to /src

import Vue from 'vue'
import {diButton} from "vuecomponentdi"
Vue.use(diButton)
export default {
  name: 'Home'
}
</script>
登录后复制

5、开发一个toast弹窗

  • toast模块:vueComponentDi/packages/toast/index.vue
<template>
    <div class="di-toast" :class="`di-toast--${type}`" v-if="show">
        {{message}}
    </div>
</template>
<script>
export default {
    data(){
        return {
            message:'',
            show:false,
            type:''
        }
    }
}
</script>
<style>
.di-toast{
    width: 60%;
    width: 200px;
    background: rgb(15, 15, 15);
    padding:3px;
    text-align: center;
    color: #fff;
    border-radius: 10px;
    position: fixed;
    left: calc(50% - 100px);
    top: 200px;
}
.di-toast--warning{
    background: #FDF6EC;
    color: #E6A28B;
}
.di-toast--success{
    background: #F0F9EB;
    color: #93C26D;
}
</style>
登录后复制
  • toast模块导出:vueComponentDi/packages/toast/index.js
import toast from './index.vue'
toast.install = (Vue) => {
    const toastConstructor = Vue.extend(toast);//使用基础 Vue 构造器,创建一个“子类”。参数是一个包含组件选项的对象。
    let $vm = new toastConstructor();//将这个子类实例化
    let $el = $vm.$mount().$el;//$vm执行$mount()手动挂载会返回一个带有$el的对象,$el就是一个dom对象
    document.querySelector("body").appendChild($el);//将这个dom对象添加到body中
    //在Vue原型上注入$toast方法
    Vue.prototype.$toast = (option) => {
        $vm.show = true
        if (!(option instanceof Object)) {
            //如果传的不是对象直接弹出
            $vm.message = option
        } else {
            $vm.message = option.message
            $vm.type = option.type
        }
        setTimeout(() => {
            $vm.show = false
        }, 2000)
    }
}


export default toast
登录后复制
  • 聚合导出:vueComponentDi/index.js
import diButton from "./packages/button" 
import toast from "./packages/toast" 

export {
    diButton,
    toast
}
登录后复制
  • vuetest中使用toast
<template>
  <div class="home">
    <di-button type="primary" @click="toast">按钮</di-button>
  </div>
</template>
<script>
// @ is an alias to /src

import Vue from "vue";
import { diButton, toast } from "vuecomponentdi";
Vue.use(diButton);
Vue.use(toast);
export default {
  name: "Home",
  methods: {
    toast() {
      // this.toast("这是一个普通弹窗");
      // this.$toast({
      //   message: "这是一个成功弹窗",
      //   type: "success",
      // });
      this.$toast({
        message: "这是一个警告弹窗",
        type: "warning",
      });
    },
  },
};
</script>
登录后复制

6、发布到npm

  • 公有配置
"publishConfig": {
    "access": "public"
  },
登录后复制
{
  "name": "vuecomponentdi",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "eslint": "^6.7.2",
    "eslint-plugin-vue": "^8.2.0"
  },
  "publishConfig": {
    "access": "public"
  }
}
登录后复制
  • 发布
npm login
npm publish
登录后复制

发布完成之后就可以在任何一个vue2项目中安装使用了:

npm install vuecomponentdi
登录后复制

git地址: vue组件开发(https://gitee.com/geeksdidi/vue-component-di)

【相关推荐:vue.js教程

以上就是如何从0撸出一个Vue组件库并发布到npm的详细内容,更多请关注Work网其它相关文章!

08-15 08:54