如何從0開發(fā)一個Vue組件庫并發(fā)布到npm

1割卖、新建文件夾在終端打開執(zhí)行 npm init -y

生成package.json如下,注意如果要發(fā)布到npm,name不能有下劃線,大寫字母等

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

2、建立目錄結(jié)構(gòu)

目錄結(jié)構(gòu)如下

-- vueComponentDi
    -- packages
        -- button
            -- index.js
            -- index.vue
        -- toast
            -- index.js
            -- index.vue
    -- index.js
    -- package.json

3敷存、本地調(diào)試

  • vueComponentDi/index.js
export default function(){
    console.log('本地調(diào)試')
}
  • 新建一個vue項目
vue create testvue

在testvue下的package.json下的測試依賴devDependencies添加vueComponentDi/index.js絕對地址

"devDependencies": {
    ...
    "vuecomponentdi": "F:/vueComponent@Di/vueComponentDi",//根據(jù)自己實際項目地址填寫
    ...
    }
  • 執(zhí)行npm link

在testvue執(zhí)行npm link將vuecomponentdi軟鏈接到node_modules中

  • vuecomponentdi安裝Eslint

由于testvue引入組件會進行Eslint檢測,不安裝會報錯(testvue關(guān)閉Eslint可省略這一步)

安裝方法:

 npm install eslint@6.7.2 --save-dev
 ./node_modules/.bin/eslint --init 
  • 在testvue使用vuecomponentdi

import test from "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>

控制臺打印>本地調(diào)試

4极舔、開發(fā)一個button組件

  • button模塊:進入vueComponentDi/packages/button/index.vue

type只支持傳入primary屬性,v-on="listeners"表示包含了父作用域中的 (不含 .native 修飾器的) v-on 事件監(jiān)聽器昆箕。它可以通過 v-on="listeners" 傳入內(nèi)部組件

<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模塊導(dǎo)出:進入vueComponentDi/packages/button/index.js

如果導(dǎo)出一個帶有install函數(shù)的對象,則在Vue2中可以直接使用Vue.use(xx)調(diào)用此函數(shù),既執(zhí)行 Vue.component(name,option)創(chuàng)建了一個組件

import button from "./index.vue"
button.install=(Vue)=>{
    Vue.component(button.name,button)
}
export default button
  • 聚合導(dǎo)出button:進入vueComponentDi/index.js

因為開發(fā)的組件不止一個,所以需要在入口文件統(tǒng)一導(dǎo)出

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似扔、開發(fā)一個toast彈窗

  • toast模塊:vueComponentDi/packages/toast/index.vue

type只支持warning和success

<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模塊導(dǎo)出:vueComponentDi/packages/toast/index.js

因為toast彈窗需要在vue中支持this.$toast調(diào)用,所以用了Vue.extend方法,這個 API 在日常開發(fā)中很少使用,一般在開發(fā)組件的時候它才能派上用場,官方定義:使用基礎(chǔ) Vue 構(gòu)造器,創(chuàng)建一個“子類”垄惧。參數(shù)是一個包含組件選項的對象

import toast from './index.vue'
toast.install = (Vue) => {
    const toastConstructor = Vue.extend(toast);//使用基礎(chǔ) Vue 構(gòu)造器刁愿,創(chuàng)建一個“子類”。參數(shù)是一個包含組件選項的對象到逊。
    let $vm = new toastConstructor();//將這個子類實例化
    let $el = $vm.$mount().$el;//$vm執(zhí)行$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
  • 聚合導(dǎo)出: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铣口、發(fā)布到npm

  • 公有配置

組件開發(fā)完成需要發(fā)布到npm以便于別人使用;因為發(fā)布的是公有包,所以需要在vueComponentDi/package.json中配置

"publishConfig": {
    "access": "public"
  },

完整package.json:

{
  "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"
  }
}

  • 發(fā)布

npm發(fā)布很簡單,只需要兩個命令:

npm login
npm publish

執(zhí)行npm login需要你有npm賬號,可以到 npm官網(wǎng) 注冊
發(fā)布完成之后就可以在任何一個vue2項目中安裝使用了:

npm install vuecomponentdi

git地址: vue組件開發(fā)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市觉壶,隨后出現(xiàn)的幾起案子脑题,更是在濱河造成了極大的恐慌,老刑警劉巖铜靶,帶你破解...
    沈念sama閱讀 222,104評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件叔遂,死亡現(xiàn)場離奇詭異,居然都是意外死亡争剿,警方通過查閱死者的電腦和手機已艰,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蚕苇,“玉大人哩掺,你說我怎么就攤上這事±κ瘢” “怎么了疮丛?”我有些...
    開封第一講書人閱讀 168,697評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長辆它。 經(jīng)常有香客問我誊薄,道長,這世上最難降的妖魔是什么锰茉? 我笑而不...
    開封第一講書人閱讀 59,836評論 1 298
  • 正文 為了忘掉前任呢蔫,我火速辦了婚禮,結(jié)果婚禮上飒筑,老公的妹妹穿的比我還像新娘片吊。我一直安慰自己,他們只是感情好协屡,可當(dāng)我...
    茶點故事閱讀 68,851評論 6 397
  • 文/花漫 我一把揭開白布俏脊。 她就那樣靜靜地躺著,像睡著了一般肤晓。 火紅的嫁衣襯著肌膚如雪爷贫。 梳的紋絲不亂的頭發(fā)上认然,一...
    開封第一講書人閱讀 52,441評論 1 310
  • 那天,我揣著相機與錄音漫萄,去河邊找鬼卷员。 笑死,一個胖子當(dāng)著我的面吹牛腾务,可吹牛的內(nèi)容都是我干的毕骡。 我是一名探鬼主播,決...
    沈念sama閱讀 40,992評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼岩瘦,長吁一口氣:“原來是場噩夢啊……” “哼未巫!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起启昧,我...
    開封第一講書人閱讀 39,899評論 0 276
  • 序言:老撾萬榮一對情侶失蹤橱赠,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后箫津,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,457評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡宰啦,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,529評論 3 341
  • 正文 我和宋清朗相戀三年苏遥,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片赡模。...
    茶點故事閱讀 40,664評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡田炭,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出漓柑,到底是詐尸還是另有隱情教硫,我是刑警寧澤,帶...
    沈念sama閱讀 36,346評論 5 350
  • 正文 年R本政府宣布辆布,位于F島的核電站瞬矩,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏锋玲。R本人自食惡果不足惜景用,卻給世界環(huán)境...
    茶點故事閱讀 42,025評論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望惭蹂。 院中可真熱鬧伞插,春花似錦、人聲如沸盾碗。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,511評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽廷雅。三九已至耗美,卻和暖如春京髓,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背幽歼。 一陣腳步聲響...
    開封第一講書人閱讀 33,611評論 1 272
  • 我被黑心中介騙來泰國打工朵锣, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人甸私。 一個月前我還...
    沈念sama閱讀 49,081評論 3 377
  • 正文 我出身青樓诚些,卻偏偏與公主長得像,于是被迫代替她去往敵國和親皇型。 傳聞我的和親對象是個殘疾皇子诬烹,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,675評論 2 359