vue 發布自己的組件庫-vue版本v2.x
參考:
vue 發布自己的組件庫-vue版本v3.x版本鏈接:https://www.cnblogs.com/zlp520/p/14691216.html
一.需求:
當遇到自己其他項目使用自己做的比較好的組件時常規的做法是ctrl+c 和ctrl+v,這種辦法倒沒有很大的問題,只不過當組件優化更新時,每個項目都要這么干,是不是很lower。於是經過查詢一堆的資料后,發現可以發布自己的組件來達到共享的目的。需求明確后,就開始如下操作,你將會擁有自己的組件庫,雷同elementui方式一樣使用了。是不是很爽。
二.初始化vue項目:
vue初始化項目有很多種方式,此處使用命令 :vue init webpack-simple,這個方式是最純凈的,因為畢竟此項目是發布組件使用,不需要過多的其他玩意。
1.vue init webpack-simple 項目名稱
三.准備文件夾及文件:
1.在src文件夾下新建components文件夾;
2.在components文件夾下新建組件文件夾里面的(ZlpAction.vue)組件文件和index.js文件
圖標識的是一個組件,多個組件雷同:

組件文件如下圖:和常規組件開發一樣,注意name必須有,並且必須規范

組件install的js,index.js如下圖:

3.在根目錄下新建index.js入口文件,index.js是為了暴露組件
根目錄:index.js簡略內容如下:
import info from './package.json' import action from './src/components/action' import actionItem from './src/components/actionItem' const components = [ action, actionItem] const install = Vue => { components.forEach(item => { Vue.component(item.name, item) }); } //引用文件方式時,會使用,類似jquery方式引入 if (typeof window !== 'undefined' && window.Vue) { install(window.Vue); } //按需導出 export { action, actionItem } //完整導出 export default { name: info.name, author: info.author, version: info.version, description: info.description, install, action, actionItem }
四.配置:
webpack.config.js配置
var path = require('path') var webpack = require('webpack') module.exports = { // entry: './src/main.js', entry:process.env.NODE_ENV=='development'? './src/main.js':'./index.js', output: { path: path.resolve(__dirname, './dist'), publicPath: '/dist/', filename: 'index.js', library:'index', libraryTarget: 'umd', // 指定輸出格式 umdNamedDefine:true }, module: { rules: [ { test: /\.css$/, use: [ 'vue-style-loader', 'css-loader' ], }, { test: /\.scss$/, use: [ 'vue-style-loader', 'css-loader', 'sass-loader' ], }, { test: /\.sass$/, use: [ 'vue-style-loader', 'css-loader', 'sass-loader?indentedSyntax' ], }, { test: /\.vue$/, loader: 'vue-loader', options: { loaders: { // Since sass-loader (weirdly) has SCSS as its default parse mode, we map // the "scss" and "sass" values for the lang attribute to the right configs here. // other preprocessors should work out of the box, no loader config like this necessary. 'scss': [ 'vue-style-loader', 'css-loader', 'sass-loader' ], 'sass': [ 'vue-style-loader', 'css-loader', 'sass-loader?indentedSyntax' ] } // other vue-loader options go here } }, { test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/ }, { test: /\.(png|jpg|gif|svg)$/, loader: 'file-loader', options: { name: '[name].[ext]?[hash]' } } ] }, resolve: { alias: { 'vue$': 'vue/dist/vue.esm.js' }, extensions: ['*', '.js', '.vue', '.json'] }, devServer: { historyApiFallback: true, noInfo: true, overlay: true }, performance: { hints: false }, //devtool: '#eval-source-map' } if (process.env.NODE_ENV === 'production') { //module.exports.devtool = '#source-map' // http://vue-loader.vuejs.org/en/workflow/production.html module.exports.plugins = (module.exports.plugins || []).concat([ new webpack.DefinePlugin({ 'process.env': { NODE_ENV: '"production"' } }), new webpack.optimize.UglifyJsPlugin({ sourceMap: true, compress: { warnings: false } }), new webpack.LoaderOptionsPlugin({ minimize: true }) ]) } else{ module.exports.devtool='#eval-source-map' }
pack.json配置:
{ "name": "zhangui", "description": "zhangui是基於vue封裝的組件庫", "version": "1.0.8", "author": "zhanglp", "license": "MIT", "private": false, "main": "./dist/index.js", "scripts": { "dev": "cross-env NODE_ENV=development webpack-dev-server --open --hot", "build": "cross-env NODE_ENV=production webpack --progress --hide-modules" }, "dependencies": { "vue": "^2.5.11" }, "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", "node-sass": "^4.5.3", "sass-loader": "^6.0.6", "vue-loader": "^13.0.5", "vue-template-compiler": "^2.4.4", "webpack": "^3.6.0", "webpack-dev-server": "^2.9.1" } }
五.測試:
打包:npm run build
npm官網地址:https://www.npmjs.com/
先去官網注冊賬號,密碼,郵箱,下面需要使用此信息
六.發布:
1.登錄命令:npm login
2.輸入賬號、密碼、郵箱
3.發布命令:npm publish(發布前切記更改pack.json文件中的版本號)
七.常見錯誤:
下載安裝使用:
錯誤1:
Cannot read property 'name' of undefined
按需導入時,組件未獲取到,原因是:沒有按需導入的方式,導出組件;
錯誤2:
生產環境導入組件失敗,提示未注冊組件
原因是:未配置webpack.config.js未配置此屬性 libraryTarget: 'umd', // 指定輸出格式
