当前位置:网站首页>Hand in hand to realize the picture preview plug-in (3)
Hand in hand to realize the picture preview plug-in (3)
2022-07-31 04:15:00 【sleeppingfrog】
因为项目需要,A global component that encapsulates a picture click preview before.This time we try to encapsulate it as a plugin.Through the imperative way to achieve the effect of click preview.
before packaging the plugin.我们要对vuehave a basic understanding of the plugin mechanism.vueThe core of the plugin isuse方法.
其实就是在Vue类上添加一个use方法,This method sees whether the first parameter passed in is a function,If it is a function, it will be executed directly,如果是对象,就看是否有install方法,然后执行install方法,执行的时候把Vue类传入,At the same time, the user is passed inoption也传入
Vue.use = function (plugin) {
// Registered plugins are ignored
if (plugin.installed) {
return
}
// 集合转数组,and remove the first parameter
var args = toArray(arguments, 1);
// 把this(即Vue)to the first parameter of the array
args.unshift(this);
// 调用install方法
if (typeof plugin.install === 'function') {
plugin.install.apply(plugin, args);
} else if (typeof plugin === 'function') {
plugin.apply(null, args);
}
// 注册成功
plugin.installed = true;
return this;
};
而在install方法里.vueProvides us with several ways to register plugins
(1) 全局minx混入 (2)全局组件注册 (3)给vueObject's prototype is added to the plugin
也就是说.The way the plug-in is called can be called in the way of the global component,也可以选择在vueCalled on the prototype or on minxCalled at some point in the mixin.
说了那么多,We started the development of our own image preview component
(1) 我们在src下新建一个plugins文件夹.新建两个文件:
prevImg.vue和index.js
<template>
<div class="prewImg-wrap" v-show="show">
<div class="mask"></div>
<div class="action">
<i class="el-icon-minus ic"></i>
<i class=" ic el-icon-plus"></i>
<i class=" ic el-icon-refresh-right"></i>
<i class=" ic el-icon-close" @click="close"></i>
</div>
<div class="img-wrap">
<span v-if="loading" style="color:#fff">加载中...</span>
<img v-else ref="img" :src="url" alt="" class="pic"
@load="handleImgLoad"
@error="handleImgError">
</div>
</div>
</template>
<script>
export default {
data(){
return {
show: false,
loading:false,
url:"https://s1.chu0.com/src/img/gif/30/30e530c90d674d26aa5afb35ab7eda84.gif"
}
},
mounted(){
this.loadImg()
},
watch:{
url(val) {
this.$nextTick(_ => {
const $img = this.$refs.img;
if (!$img.complete) {
this.loading = true;
}
});
}
},
methods:{
close(){
this.show=false
},
handleImgLoad(){
this.loading=false
},
handleImgError(e){
console.log("e-rror--",e)
this.loading=false
}
}
}
</script>
<style scoped>
.prewImg-wrap{
position:fixed;
left:0;
top:0;
width: 100%;
height: 100%;;
overflow: hidden;
z-index:1;
}
.action{
width: 200px;
padding: 20px;
position: absolute;
right:10px;
top:20px;
color:#fff;
z-index: 30;
display: flex;
justify-content: flex-end;
}
.ic{
display: inline-block;
margin-right: 10px;
}
.mask{
background: rgba(0,0,0,0.6);
position: absolute;
top:0;
left:0;
z-index: 10;
width: 100%;
height: 100%;
}
.img-wrap{
position: absolute;
top:0;
left:0;
width: 100%;
height: 100%;
z-index:20;
display: flex;
justify-content: center;
align-items: center;
}
.pic{
max-height: 100%;
max-width: 100%;
}
</style>
prevImg/index.js
import PrevView from "./prewImg.vue"
export default {
install(Vue,options){
let PrevVueConstructor= Vue.extend(PrevView)
let instance= new PrevVueConstructor()
document.body.appendChild(instance.$mount().$el);
let prevObj={
show(opts={text:"",url:""}){
console.log("instance:",instance)
instance.show=true
instance.text=opts.text||""
instance.url=opts.url||""
},
hide(){
instance.show=false
instance=null
}
}
if(!Vue.$prevObj){
Vue.$prevObj=prevObj
}
Vue.prototype.$prevImg = prevObj
}
}This file makes us the core of this plugin.首先我们通过vue.extendConstructs a basevue的构造函数,它类似于vue的实例.具备vue实例的所有属性和方法,和根vue实例不同的是,通过extendConstructed subclasses onlydata只能是一个函数.我们创建Vue实例时,都会有一个el选项,to specify the root node of the instance,如果不写el选项,That component is in an unmounted state.Vue.extend 的作用,就是基于 Vue 构造器,创建一个‘ 子类 ',再配合$mount,The component can be rendered,And mount it on any specified node,比如body(This is not possible with single-file components)
在show方法中,console.log("instance:",instance)can see its specific structure.也正是因为它.So we have direct access to usprewImg组件中data定义的数据.比如show,我们就可以通过操作show变量来控制open和hidemethod directlyprevImgComponents are shown or hidden.
最后我们把这个prevImg对象挂载到vue的原型上.
(2)main.js中引入 prevImg文件夹下的index.js
import Vue from 'vue'
import App from './App.vue'
import "./directives/composImg"
Vue.config.productionTip = false
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import prewImg from "./plugins/prewImg"
Vue.use(ElementUI);
Vue.use(prewImg)
new Vue({
render: h => h(App),
}).$mount('#app')
(3)具体使用
<el-button @click="openPrev">打开预览</el-button>
<el-button @click="closePrve">关闭预览</el-button>
<script>
export default{
data(){
return{
restaurants: [],
state1: '',
url:"https://baj-dabanjiz-conf.oss-cn-hangzhou.aliyuncs.com/intelligent-design/image/20210730/middle/9bbeb6570f7b416b1bcbcc59a1b38635.jpg",
url2:"https://fuss10.elemecdn.com/8/27/f01c15bb73e1ef3793e64e6b7bbccjpeg.jpeg"
}
},
}
methods:{
openPrev(){
this.$prevImg.show({url:this.url2})
},
closePrve(){
this.$prevImg.hide()
},
}
</script>

边栏推荐
- beforeDestroy与destroyed的使用
- Solved (the latest version of selenium framework element positioning error) NameError: name 'By' is not defined
- [Paper reading] Mastering the game of Go with deep neural networks and tree search
- 【论文阅读】Mastering the game of Go with deep neural networks and tree search
- 专访 | 阿里巴巴首席技术官程立:云+开源共同形成数字世界的可信基础
- Error EPERM operation not permitted, mkdir ‘Dsoftwarenodejsnode_cache_cacach两种解决办法
- "DeepJIT: An End-To-End Deep Learning Framework for Just-In-Time Defect Prediction" paper notes
- 聚变云原生,赋能新里程 | 2022开放原子全球开源峰会云原生分论坛圆满召开
- $parent/$children 与 ref
- Daily practice of LeetCode - 138. Copy a linked list with random pointers
猜你喜欢
![Summary of Huawei Distributed Storage FusionStorage Knowledge Points [Interview]](/img/83/e0163b324448c6ef5b106862673637.jpg)
Summary of Huawei Distributed Storage FusionStorage Knowledge Points [Interview]
![[Swift]自定义点击APP图标弹出的快捷方式](/img/d4/84b237995fc3d3700916eb57f6670d.png)
[Swift]自定义点击APP图标弹出的快捷方式

(5) final, abstract class, interface, inner class

高等数学---第九章二重积分

Exsl file preview, word file preview web page method

马斯克对话“虚拟版”马斯克,脑机交互技术离我们有多远

强化学习:从入门到入坑再到拉屎

pom文件成橘红色未加载的解决方案

The BP neural network

BUG消灭者!!实用调试技巧超全整理
随机推荐
IDEA common shortcut keys and plug-ins
ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your
Regarding the primary key id in the mysql8.0 database, when the id is inserted using replace to be 0, the actual id is automatically incremented after insertion, resulting in the solution to the repea
Win10 CUDA CUDNN 安装配置(torch paddlepaddle)
C语言表白代码?
Port inspection steps - 7680 port analysis - Dosvc service
三子棋的代码实现
Redis 使用LIST做最新评论缓存
C language confession code?
[Swift]自定义点击APP图标弹出的快捷方式
The use of beforeDestroy and destroyed
开放原子开源基金会秘书长孙文龙 | 凝心聚力,共拓开源
A brief introduction to the showDatePicker method of the basic components of Flutter
BP神经网络
Understanding and Using Unity2D Custom Scriptable Tiles (4) - Start to build a custom tile based on the Tile class (below)
beforeDestroy与destroyed的使用
《DeepJIT: An End-To-End Deep Learning Framework for Just-In-Time Defect Prediction》论文笔记
问题7:列表的拼接
The BP neural network
产学研用 共建开源人才生态 | 2022开放原子全球开源峰会教育分论坛圆满召开