Vue自定义指令的用法

112 阅读1分钟

vue directive自定义指令的使用

  1. 自定义指令图片懒加载实例
vue.directive('image',{
	inserted(el,bind){
	let color = Math.floor(Math.random()*1000);
	el.style.background = '#' +color;
	var image = new image();
	image.src = bind.value;
	image.onload(){
		el.style.backgroundimage = 'url+('+ bind.value')'
		}
	}
})

vue页面渲染

<template>
	<view v-image='item.src' v-for="(item,index) in imagelist" :key="item.id">
	<view/>
</template>