vue 發布自己的組件庫-vue版本v3.x
一.需求:
當遇到自己其他項目使用自己做的比較好的組件時常規的做法是ctrl+c 和ctrl+v,這種辦法倒沒有很大的問題,只不過當組件優化更新時,每個項目都要這么干,是不是很lower。於是經過查詢一堆的資料后,發現可以發布自己的組件來達到共享的目的。需求明確后,就開始如下操作,你將會擁有自己的組件庫,雷同elementui方式一樣使用了。是不是很爽。
二.初始化vue項目:
vite方式初始化vue3.x項目 https://www.cnblogs.com/zlp520/p/14694599.html
三.准備文件夾及文件:
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配置:
五.測試:
打包: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', // 指定輸出格式