vue版本裁切工具,包含預覽功能
最終效果: qiuyaofan.github.io/vue-crop-de…
源碼地址: github.com/qiuyaofan/v…
第一步:先用vue-cli安裝腳手架(不會安裝的看 vue-cli官網)
// 初始化vue-clivue init webpack my-plugin
第二步:創建文件
新建src/views/validSlideDemo.vue,
src/components里新建VueCrop/index.js,VueCrop.vue,
在routes/index.js配置訪問路由(具體看github源碼)
最終生成的文件結構如下圖:
第三步:注冊組件
1.引用所有插件:src/components/index.js
// 導入插件入口文件import VueCrop from './VueCrop/index.js'const install = function (Vue, opts = {}) { /* 如果已安裝就跳過 */ if (install.installed) return // 注冊插件 Vue.component(VueCrop.name, VueCrop)}// 全局情況下注冊插件if (typeof window !== 'undefined' && window.Vue) { install(window.Vue)}export { install, // 此處是為了兼容在vue內單獨引入這個插件,如果是main.js全局引入就可以去掉 VueCrop}2.全局調用插件:src/main.js ( vue plugins官方文檔解說install )
import Vue from 'vue'import App from './App'import router from './router'// 新加的:導入入口文件import { install } from 'src/components/index.js'// 全局調用,相當于調用 `MyPlugin.install(Vue)`Vue.use(install)Vue.config.productionTip = false/* eslint-disable no-new */new Vue({ el: '#app', router, components: { App }, template: '<App/>'})3.VueCrop入口文件調用VueCrop.vue:src/components/VueCrop/index.js
// 導入vueimport VueCrop from './VueCrop.vue'// Vue.js 的插件應當有一個公開方法 install 。這個方法的第一個參數是 Vue 構造器VueCrop.install = function (Vue) { // 注冊組件 Vue.component(VueCrop.name, VueCrop)}export default VueCrop小結:我一開始一直有個誤解,以為myPlugin.install是vue的一個方法,其實不是,他只是我們構造plugin識的一個公開方法,可以理解為原生js中的構造函數的方法:
function MyPlugin(){ console.info('構造函數')}MyPlugin.prototype.install=function(vue,options){ console.info('構造器vue:'+vue);}而真正注冊組件的是:Vue.component()
所以,vue插件注冊的過程是:
1.調用main.js中:
import { install } from 'src/components/index.js'vue.use(install)2.index.js添加install方法,調用Vue.component注冊組件
3.組件內的index.js同所有組件的index.js一樣
第四步:設計開發自己的組件,構建組件結構
在此之前,可以先了解下組件的命名規范等,可參考文章 掘金:Vue前端開發規范 ,其中第2點有詳細講解
首先,確定自己的調用方式和需要暴露的參數
<vue-crop:crop-url="cropUrl1":ratio="ratio":height="460":width="460":previewJson="previewJson1"class="c-crop--preview_right"@afterCrop="afterCrop">>
其中,@afterCrop="afterCrop"是裁切完成的回調函數,其他是屬性配置
在組件src/components/VueCrop/VueCrop.vue內,可以用this.$emit('afterCrop')觸發demo里的afterCrop事件
組件結構上,主要分為:裁切主要部分,選框組件(VueCropTool.vue),裁切框寬度、位置坐標等計算(VueCropMove.js),拖拽事件注冊公共js(components/utils/draggable.js)
draggable.js是參照element里的,修改了一部分,源碼如下
export default function (element, options) { const moveFn = function (event) { if (options.drag) { options.drag(event) } } // mousedown fn const downFn = function (event) { if (options.start) { // 調用參數中start函數 options.start(event) } } // mouseup fn const upFn = function (event) { document.removeEventListener('mousemove', moveFn) document.removeEventListener('mouseup', upFn) document.onselectstart = null document.ondragstart = null if (options.end) { // 調用參數中end函數 options.end(event) } } // 綁定事件 element.addEventListener('mousedown', event => { if (options.stop && options.stop(event, element) === false) { return false } document.onselectstart = function () { return false } document.ondragstart = function () { return false } document.addEventListener('mousedown', downFn) document.addEventListener('mousemove', moveFn) document.addEventListener('mouseup', upFn) })}總結
以上所述是小編給大家介紹的vue裁切預覽組件功能的實現步驟,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!
新聞熱點
疑難解答