您好,欢迎访问一九零五行业门户网

如何使用Vue2x实现图片预览插件

这次给大家带来如何使用vue2x实现图片预览插件,使用vue2x实现图片预览插件的注意事项有哪些,下面就是实战案例,一起来看一下。
先来看下demo
livedemo
关于开发vue插件的几种方式 (具体请移步官网)vue官网
myplugin.install = function (vue, options) {  // 1. 添加全局方法或属性  vue.myglobalmethod = function () {   // 逻辑...  }  // 2. 添加全局资源  vue.directive('my-directive', {   bind (el, binding, vnode, oldvnode) {    // 逻辑...   }   ...  })  // 3. 注入组件  vue.mixin({   created: function () {    // 逻辑...   }   ...  })  // 4. 添加实例方法  vue.prototype.$mymethod = function (methodoptions) {   // 逻辑...  } }
我采用第一种方式来编写这个插件
1.第一步创建项目
vue init webpack-simple youprojectname(你的项目名称)具体操作不在赘述
2.开始插件开发,编写index.js
import vuepictureviewer from './vue-picture-viewer' const pictureviewer = {  install (vue, options) {   vue.component(vuepictureviewer.name, vuepictureviewer)  } } if (typeof window !== 'undefined' && window.vue) { // 这段代码很重要  window.vue.use(pictureviewer) } export default pictureviewer
3.编写vue-picture-viewer.vue也挺简单(具体可以去看源码)
4.如何使用(main.js)
import vuepictureviewer from './lib/index.js' vue.use(vuepictureviewer)
app.vue
<template>  <p id="app">   <vue-picture-viewer :imgdata="imgurl" :switch="true" v-if="imgurl"></vue-picture-viewer>  </p> </template> <script> export default {  name: 'app',  data () {   return {    imgurl: [{      url:'http://p8ny46w8x.bkt.clouddn.com/test1.jpg',      name: 'test1.jpg'     },     {      url: 'http://p8ny46w8x.bkt.clouddn.com/test2.jpg',      name: 'test2.jpg'     }, {      url: 'http://p8ny46w8x.bkt.clouddn.com/test3.jpg',      name: 'test3.jpg'     },     {      url: 'http://p8ny46w8x.bkt.clouddn.com/test4.jpg',      name: 'test4.jpg'     }]   }  } } </script> <style> * {  margin: 0;  padding: 0; } html, body {  width: 100%;  height: 100%; } </style>
5.打包前的配置webpack.config.js(很重要!!!)
module.exports = {  entry: './src/lib/index.js',  output: {   path: path.resolve(dirname, './dist'),   publicpath: '/dist/',   // filename: 'build.js',   filename: 'vue-picture-viewer.js',   library: 'pictureviewer',   librarytarget: 'umd',   umdnameddefine: true  },
6.打包成功,配置package.json
license: mit, // 许可证  private: false, // 默认是true 私人的 需要改为false, 不然发布不成功!  main: dist/vue-picture-viewer.js, 这个超级重要 决定了你 import xxx from “vue-picture-viewer” 它默认就会去找 dist下的vue-picture-viewer 文件  repository: {   type: git,   url: https://github.com/sangcz/vue-picture-viewer // github项目地址  },
7.一切ok准备发布!
8.首先注册好npm后 添加用户
npm adduser  username: your name  password: your password  email: yourmail // 查看一下登录的是不是你自己 npm whoami // 发布 npm publish // 这里我遇到一个问题,发布失败了!
什么原因呢?
9.解决了上面的问题,发布成功了!开心
以上就是如何使用vue2x实现图片预览插件的详细内容。
其它类似信息

推荐信息