网络编程 
首页 > 网络编程 > 浏览文章

使用vue-cli编写vue插件的方法

(编辑:jimmy 日期: 2024/11/19 浏览:3 次 )

利用vue组件创建模板,使用webpack打包生成插件再全局使用

1、vue init webpack-simple 生成项目目录

2、调整目录结构

使用vue-cli编写vue插件的方法

3、修改webpack.config.js

var path = require('path')
var webpack = require('webpack')

module.exports = {
 entry: './src/index.js',
 output: {
  path: path.resolve(__dirname, './dist'),
  publicPath: '/dist/',
  filename: 'vue-toast.js',
  // 打包后的格式(三种规范amd,cmd,common.js)通过umd规范可以适应各种规范,以及全局window属性
  libraryTarget:'umd',
 },
 module: {
  rules: [
   {
    test: /\.vue$/,
    loader: 'vue-loader',
   },
   {
    test: /\.js$/,
    loader: 'babel-loader',
    exclude: /node_modules/
   },
   
  ]
 },
 plugins:[]
} 

开发一个toast插件,可以借助npm平台发布,在这里就不做过多的说明了

toast.vue

<template>
 <transition name="toast-fade">
  <div class="toast"
   :class="objClass" 
   v-show="isActive"
   @mouseenter="onMouseenter"
   @mouseleave="onMouseleave"
   >
   <button class="toast-close-button" @click="hide">×</button>
   <div class="toast-container">
    <div class="toast-title">{{title}}</div>
    <div class="toast-content">{{content}}</div>
   </div>
  </div>
 </transition>
</template>

<script>
export default {
 data: () => ({
  list: [],
  title: null,
  content: null,
  type: null,
  isActive: false,
  timer: null,
  onShow: () => {},
  onHide: () => {}
 }),
 computed: {
  objClass () {
   // 样式'success, error, warning, default'
   return this.type "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAADsSURBVEhLY2AYBfQMgf///3P8+/evAIgvA/FsIF+BavYDDWMBGroaSMMBiE8VC7AZDrIFaMFnii3AZTjUgsUUWUDA8OdAH6iQbQEhw4HyGsPEcKBXBIC4ARhex4G4BsjmweU1soIFaGg/WtoFZRIZdEvIMhxkCCjXIVsATV6gFGACs4Rsw0EGgIIH3QJYJgHSARQZDrWAB+jawzgs+Q2UO49D7jnRSRGoEFRILcdmEMWGI0cm0JJ2QpYA1RDvcmzJEWhABhD/pqrL0S0CWuABKgnRki9lLseS7g2AlqwHWQSKH4oKLrILpRGhEQCw2LiRUIa4lwAAAABJRU5ErkJggg==") !important;
}
.toast-error {
 color: #fff;
 background-color: #bd362f;
 padding: 15px 15px 15px 50px;
 background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAHOSURBVEhLrZa/SgNBEMZzh0WKCClSCKaIYOED+AAKeQQLG8HWztLCImBrYadgIdY+gIKNYkBFSwu7CAoqCgkkoGBI/E28PdbLZmeDLgzZzcx83/zZ2SSXC1j9fr+I1Hq93g2yxH4iwM1vkoBWAdxCmpzTxfkN2RcyZNaHFIkSo10+8kgxkXIURV5HGxTmFuc75B2RfQkpxHG8aAgaAFa0tAHqYFfQ7Iwe2yhODk8+J4C7yAoRTWI3w/4klGRgR4lO7Rpn9+gvMyWp+uxFh8+H+ARlgN1nJuJuQAYvNkEnwGFck18Er4q3egEc/oO+mhLdKgRyhdNFiacC0rlOCbhNVz4H9FnAYgDBvU3QIioZlJFLJtsoHYRDfiZoUyIxqCtRpVlANq0EU4dApjrtgezPFad5S19Wgjkc0hNVnuF4HjVA6C7QrSIbylB+oZe3aHgBsqlNqKYH48jXyJKMuAbiyVJ8KzaB3eRc0pg9VwQ4niFryI68qiOi3AbjwdsfnAtk0bCjTLJKr6mrD9g8iq/S/B81hguOMlQTnVyG40wAcjnmgsCNESDrjme7wfftP4P7SP4N3CJZdvzoNyGq2c/HWOXJGsvVg+RA/k2MC/wN6I2YA2Pt8GkAAAAASUVORK5CYII=") !important;
}
.toast-warning {
 color: #fff;
 background-color: #f89406;
 padding: 15px 15px 15px 50px;
 background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAGYSURBVEhL5ZSvTsNQFMbXZGICMYGYmJhAQIJAICYQPAACiSDB8AiICQQJT4CqQEwgJvYASAQCiZiYmJhAIBATCARJy+9rTsldd8sKu1M0+dLb057v6/lbq/2rK0mS/TRNj9cWNAKPYIJII7gIxCcQ51cvqID+GIEX8ASG4B1bK5gIZFeQfoJdEXOfgX4QAQg7kH2A65yQ87lyxb27sggkAzAuFhbbg1K2kgCkB1bVwyIR9m2L7PRPIhDUIXgGtyKw575yz3lTNs6X4JXnjV+LKM/m3MydnTbtOKIjtz6VhCBq4vSm3ncdrD2lk0VgUXSVKjVDJXJzijW1RQdsU7F77He8u68koNZTz8Oz5yGa6J3H3lZ0xYgXBK2QymlWWA+RWnYhskLBv2vmE+hBMCtbA7KX5drWyRT/2JsqZ2IvfB9Y4bWDNMFbJRFmC9E74SoS0CqulwjkC0+5bpcV1CZ8NMej4pjy0U+doDQsGyo1hzVJttIjhQ7GnBtRFN1UarUlH8F3xict+HY07rEzoUGPlWcjRFRr4/gChZgc3ZL2d8oAAAAASUVORK5CYII=") !important;
}
.toast:hover {
 opacity: 1;
 box-shadow: 0 0 18px #888;
 transition: all 200ms ease;
}
.toast-container {
 vertical-align: middle;
}

.toast-fade-enter, .toast-fade-leave-active {
 opacity: 0;
 transform: translateX(100%);
}
.toast-fade-leave-active,
.toast-fade-enter-active {
 transition: all 400ms cubic-bezier(.36,.66,.04,1);
}
.toast-title {
 font-size: 14px;
 font-weight: bold;
}
.toast-close-button {
  padding: 2px 2px;
  border: none;
  background: transparent;
  position: relative;
  right: -10px;
  top: -15px;
  float: right;
  font-size: 20px;
  font-weight: bold;
  color: #ffffff;
  -webkit-text-shadow: 0 1px 0 #ffffff;
  text-shadow: 0 1px 0 #ffffff;
  -ms-filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=80);
  filter: alpha(opacity=80);
}
</style>

index.js

import ToastComponent from './toast.vue'
let Toast = {};
Toast.install = function(Vue, options = {}) {
  // extend组件构造器
  const VueToast = Vue.extend(ToastComponent)
  let toast = null
  function $toast(params) {
    return new Promise( resolve => {
      if(!toast) {
        toast = new VueToast()
        toast.$mount()
        document.querySelector(options.container || 'body').appendChild(toast.$el)
      }
      toast.show(params)
      resolve()
    })
  }
  Vue.prototype.$toast = $toast
}
if(window.Vue){
  Vue.use(Toast)
}
export default Toast

npm run build 之后就会在根目录下生成dist文件

使用vue-cli编写vue插件的方法

接下来就可以使用了

demo.html

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
 <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no">
 <!--引入-->
 <script src="/UploadFiles/2021-04-02/vue.js">

总结:

1、使用Vue构造器,通过vue组件来创建一个子类:Vue.extend(component)

2、webpack配置output的path必须为绝对路径

3、webpack基础配置:entry,output,module,plugins

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:vue-cli下的vuex的简单Demo图解(实现加1减1操作)
下一篇:使用ngrok+express解决本地环境中微信接口调试问题
一文看懂荣耀MagicBook Pro 16
荣耀猎人回归!七大亮点看懂不只是轻薄本,更是游戏本的MagicBook Pro 16.
人们对于笔记本电脑有一个固有印象:要么轻薄但性能一般,要么性能强劲但笨重臃肿。然而,今年荣耀新推出的MagicBook Pro 16刷新了人们的认知——发布会上,荣耀宣布猎人游戏本正式回归,称其继承了荣耀 HUNTER 基因,并自信地为其打出“轻薄本,更是游戏本”的口号。
众所周知,寻求轻薄本的用户普遍更看重便携性、外观造型、静谧性和打字办公等用机体验,而寻求游戏本的用户则普遍更看重硬件配置、性能释放等硬核指标。把两个看似难以相干的产品融合到一起,我们不禁对它产生了强烈的好奇:作为代表荣耀猎人游戏本的跨界新物种,它究竟做了哪些平衡以兼顾不同人群的各类需求呢?