網(wǎng)上看了一些文章,一般都是把封裝好的.vue格式的文件發(fā)布到npm蜂怎,感覺(jué)不太像一般組件的封裝形式怕磨,故特意書(shū)寫(xiě)此文喂饥,希望能幫助到各位碼友。
本文以封裝一個(gè)分頁(yè)組件為例癌压,把它build生成目標(biāo)js文件仰泻,pack生成壓縮包測(cè)試,并發(fā)布到npm滩届。
項(xiàng)目初始化
封裝vue的插件用webpack-simple很合適,vue init webpack-simple vue-gitment 此命令創(chuàng)建我們的項(xiàng)目的目錄被啼,創(chuàng)建文件夾和文件帜消,最后結(jié)構(gòu)是這樣的lib目錄是我們的插件目錄,其他的默認(rèn)就好浓体。
組件內(nèi)容
Pagination.vue 的內(nèi)容如下:
<template>
<div class="clearfix box_pagination">
<div class="laypage_l">
<label>第 </label>
<label>{{ current }}</label><label>/{{ pagegroup }} 頁(yè)泡挺,共{{ total }}條,每頁(yè)顯示{{ display }}條</label>
</div>
<div class="laypage_r">
<ul class="pagination">
<li :class="{'disabled': current == 1}" v-show="isShowFirst"><a href="javascript:;" class="single" @click="setCurrent(1)"> 首頁(yè) </a></li>
<li :class="{'disabled': current == 1}" v-show="isShowPrev"><a href="javascript:;" class="prev" @click="setCurrent(current - 1)"> 上一頁(yè) </a></li>
<li v-for="p in showPages" :class="{'active': current == p.val}">
<a href="javascript:;" @click="setCurrent(p.val)" v-if="p.text==='...'" class="noBorder">{{ p.text }}</a>
<a href="javascript:;" @click="setCurrent(p.val)" v-else> {{ p.text }} </a>
</li>
<li :class="{'disabled': current == page}" v-show="isShowNext"><a href="javascript:;" class="next" @click="setCurrent(current + 1)"> 下一頁(yè) </a></li>
<li :class="{'disabled': current == page}" v-show="isShowLast"><a href="javascript:;" class="single" @click="setCurrent(page)"> 尾頁(yè) </a></li>
</ul>
</div>
</div>
</template>
<script>
export default{
data(){
return {
current: this.currentPage,
showPages: [],
isShowFirst: false,
isShowPrev: false,
isShowNext: true,
isShowLast: true
}
},
props: {
total: {// 數(shù)據(jù)總條數(shù)
type: Number,
default: 0
},
display: {// 每頁(yè)顯示條數(shù)
type: Number,
default: 10
},
currentPage: {// 當(dāng)前頁(yè)碼
type: Number,
default: 1
},
pagegroup: {// 分頁(yè)條數(shù)
type: Number,
default: 1,
coerce: function (v) {
v = v > 0 ? v : 5;
return v % 2 === 1 ? v : v + 1;
}
}
},
computed: {
page: function () { // 總頁(yè)數(shù)
return Math.ceil(this.total / this.display);
}
},
methods: {
getPagesList() {
let self= this;
let len = self.page, temp = [];
if(len <= 5) { // 當(dāng)頁(yè)數(shù)小于5
while (len--) {
temp.push({text: self.page - len, val: self.page - len});
}
} else {
for(let i=0;i<5;i++) {
len--;
temp.push({text: self.page - len, val: self.page - len});
}
temp.push({text: '...', val: 6});
}
self.showPages = temp;
},
setCurrent: function (idx) {
let self = this;
if(self.current != idx && idx > 0 && idx < self.page + 1) {
self.current = idx;
if(idx === 1) {
self.isShowFirst = false;
self.isShowPrev = false;
self.isShowNext = true;
self.isShowLast = true;
} else if (idx == self.page) {
self.isShowFirst = true;
self.isShowPrev = true;
self.isShowNext = false;
self.isShowLast = false;
} else {
self.isShowFirst = true;
self.isShowPrev = true;
self.isShowNext = true;
self.isShowLast = true;
}
if(idx > 5) {
let temp_arry = [], start = idx - 2, end = idx + 2;
if(end>=self.page) {
start = idx - (5 - (self.page-idx) - 1);
end = self.page;
}
temp_arry.push({text: '...', val: idx - 3});
for(let i=start;i<=end;i++) {
temp_arry.push({text: i, val: i});
}
if(idx < (self.page-5/2)) {
temp_arry.push({text: '...', val: idx + 3});
}
self.showPages = temp_arry;
} else {
let temp_arry = [];
for(let i=1;i<=idx;i++) {
temp_arry.push({text: i, val: i});
}
if(idx === 5) {
temp_arry.push({text: '...', val: idx + 1});
}
self.showPages = temp_arry;
}
this.$emit('pagechange', this.current);
}
}
},
mounted() {
this.getPagesList();
},
}
</script>
<style>
.box_pagination {
padding: 20px 0px;
}
.laypage_l {
float: left;
width: 30%;
}
.laypage_l label {
color: #999;
}
.laypage_r {
float: right;
width: 70%;
}
.pagination {
overflow: hidden;
margin: 0 auto;
height: 50px;
text-align: right;
}
.pagination li {
display: inline-block;
height: 34px;
margin: 0px 3px;
color: #666;
}
.pagination li .noBorder {
border: none;
}
.pagination li:hover {
}
.pagination li:hover a {
color: #25aae1;
background: #ebfaff;
}
.pagination li a {
display: block;
width: 34px;
height: 34px;
color: #333;
text-align: center;
line-height: 32px;
font-size: 14px;
text-decoration: none;
border: 2px solid #ddd;
background-color: #f9fbfc;
}
.pagination li a.single {
width: 54px;
}
.pagination li a.prev {
width: 68px;
}
.pagination li a.next {
width: 68px;
}
.pagination .active a {
color: #25aae1;
background: #ebfaff;
border: 2px solid #b8dbea;
}
</style>
index.js 用來(lái)安裝組件命浴,內(nèi)容如下:
import Pagination from './Pagination.vue'
const pagination={
install:function (Vue) {
Vue.component('vPagination',Pagination)
}
};
// 這里的判斷很重要
if (typeof window !== 'undefined' && window.Vue) {
window.Vue.use(pagination)
}
export default pagination
配置文件
package.json 內(nèi)容如下:
{
"name": "pagination",
"description": "A Vue.js project",
"version": "1.0.0",
"author": "author",
"license": "MIT",
"private": false,
"scripts": {
"dev": "cross-env NODE_ENV=development webpack-dev-server --open --hot",
"build": "cross-env NODE_ENV=production webpack --progress --hide-modules"
},
"directories": {
"dist": "dist"
},
"dependencies": {
"vue": "^2.4.4"
},
"browserslist": [
"> 1%",
"last 2 versions",
"not ie <= 8"
],
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-env": "^1.6.0",
"babel-preset-stage-3": "^6.24.1",
"cross-env": "^5.0.5",
"css-loader": "^0.28.7",
"file-loader": "^1.1.4",
"vue-loader": "^13.0.5",
"vue-template-compiler": "^2.4.4",
"webpack": "^3.6.0",
"webpack-dev-server": "^2.9.1"
}
}
webpack.config.js 內(nèi)容如下:
module.exports = {
entry: './src/lib/index.js',
output: {
path: path.resolve(__dirname, './dist/js'),
publicPath: '/dist/',
filename: 'pagination.js',
library: 'pagination',
libraryTarget: 'umd',
umdNamedDefine: true
}娄猫,
...... // 其余默認(rèn)就好
};
entry:修改打包的入口文件贱除。
output:修改輸出文件到 dist/js下,生成文件名為pagination.js媳溺。
library:指定的就是你使用require時(shí)的模塊名月幌,這里便是require("pagination")。
libraryTarget:會(huì)生成不同umd的代碼,可以只是commonjs標(biāo)準(zhǔn)的悬蔽,也可以是指amd標(biāo)準(zhǔn)的扯躺,也可以只是通過(guò)script標(biāo)簽引入的。
umdNamedDefine:會(huì)對(duì) UMD 的構(gòu)建過(guò)程中的 AMD 模塊進(jìn)行命名蝎困。否則就使用匿名的 define录语。
build
控制臺(tái)輸入 npm run build,會(huì)生成下列文件:
pack打包生成壓縮包
修改package.json 內(nèi)容如下:
{
"name": "pagination",
"description": "A vue.js 2.0 project on asynchronous paging ",
"version": "0.1.0",
"author": "y",
"license": "MIT",
"private": false,
"main": "dist/js/pagination.js",
"directories": {
"dist": "dist"
},
"files": [
"dist",
"src"
],
"scripts": {
"dev": "node build/dev-server.js",
"start": "node build/dev-server.js",
"build": "node build/build.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/yanzilingyan/vue.git"
},
"keywords": [
"vue",
"pagination",
"ajax pagination"
],
"dependencies": {
"vue": "^2.4.4"
},
"browserslist": [
"> 1%",
"last 2 versions",
"not ie <= 8"
],
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-env": "^1.6.0",
"babel-preset-stage-3": "^6.24.1",
"cross-env": "^5.0.5",
"css-loader": "^0.28.7",
"file-loader": "^1.1.4",
"vue-loader": "^13.0.5",
"vue-template-compiler": "^2.4.4",
"webpack": "^3.6.0",
"webpack-dev-server": "^2.9.1"
}
}
控制臺(tái)輸入 npm pack禾乘,會(huì)看到在當(dāng)前工程目錄下生成了一個(gè)文件 pagination-0.1.0.tgz
項(xiàng)目測(cè)試引入
另外新建一個(gè)工程項(xiàng)目澎埠,vue init webpack vue-ptest 此命令創(chuàng)建我們的項(xiàng)目的目錄,創(chuàng)建文件夾和文件始藕,類(lèi)似如下結(jié)構(gòu):
把剛才打包好的 pagination-0.1.0.tgz 這個(gè)壓縮包放到E盤(pán)目錄下(當(dāng)然哪個(gè)位置都行失暂,這里個(gè)人只是覺(jué)得方便些)。在當(dāng)前 vue-ptest 這個(gè)工程目錄下鳄虱,打開(kāi)控制臺(tái)弟塞,輸入 npm install --save-dev E:pagination-0.1.0.tgz , 安裝剛才打包好的文件包拙已。
修改main.js文件决记,引入此包,內(nèi)容如下:
import Vue from 'vue'
import App from './App'
import router from './router'
import vPagination from 'pagination'
Vue.config.productionTip = false;
Vue.use(vPagination);
new Vue({
el: '#app',
router,
template: '<App/>',
components: { App }
});
修改HelloWorld.vue文件倍踪,引入分頁(yè)組件系宫,內(nèi)容如下:
<template>
<div class="hello">
<h1>{{ msg }}</h1>
<h2>Essential Links</h2>
<vPagination :total="total" :current-page='current' :pagegroup="pagegroup" @pagechange="pagechange" v-if="isShowPagination"></vPagination>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data () {
return {
msg: 'Welcome to Your Vue.js App',
total: 1,
current: 1,
pagegroup: 10,
isShowPagination: true
}
},
methods: {
pagechange(currentPage) {
console.log('currentPage=',currentPage);
}
}
}
</script>
<style scoped>
h1, h2 {
font-weight: normal;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
</style>
打開(kāi)控制臺(tái),輸入 npm run dev 建车,啟動(dòng)扩借。
看到界面如下顯示:
OK,分頁(yè)組件顯示正常缤至。
使用Verdaccio搭建私有npm倉(cāng)庫(kù)
安裝nrm 用來(lái)切換npm的源潮罪。 安裝方法:npm install -g nrm
在控制臺(tái)輸入verdaccio 就可以啟動(dòng)
修改配置文件
打開(kāi) /***/verdaccio/config.yaml 在文檔的末尾加上(已有的修改): listen:XX.XX.XX.XX:4873
(XX.XX.XX.XX 這個(gè)ip盡量能做到在正式環(huán)境、測(cè)試環(huán)境领斥、開(kāi)發(fā)環(huán)境都可以訪問(wèn)到的嫉到。) 修改后 重新啟動(dòng) verdaccio
新建npm 源 nrm add (名) http:XX.XX.XX.XX:4873
切換npm源 nrm use (名)
新建用戶(hù) npm adduser 根據(jù)提示創(chuàng)建用戶(hù)名、密碼 email
發(fā)布私有包的過(guò)程
1.切換npm 源 確定是在內(nèi)網(wǎng)源上例如((名) http:XX.XX.XX.XX:4873)
2.npm publish 在自己要發(fā)布的包中路徑下打這個(gè)命令
3.進(jìn)入 http:XX.XX.XX.XX:4873 是否成功
4.成功的話(huà)在你的項(xiàng)目里 npm install -seve-dev (vue-gitment項(xiàng)目名)
安裝包
5.確定是在私有源下(如果需要安裝私有包的時(shí)候)
?? 我姐
內(nèi)容原: vuejs2.0 封裝通用組件月洛,build生成js何恶,并發(fā)布到npm