例如一个产品详情中,后台动态返回一系列数据,需要在里面识别出img标签,让其懒加载
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<div id="app">
<com1 :com="com"></com1>
</div>
<template id="temp1">
<div>
<img v-d1 src="https://cn.vuejs.org/images/logo.png" />
</div>
</template>
<script src="http://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script src="http://cdn.bootcss.com/vue/2.4.2/vue.min.js"></script>
<script>
Vue.directive('d1', {
// 当绑定元素插入到 DOM 中。
inserted: function (el) {
console.log('inserted')
}
})
Vue.component('com1', {
functional: true,
render: function (createElement, context) {
return createElement(context.props.com)
},
props: {
com: Function
}
})
function GetCom(html) {
const dom = $(html)
dom.find('img').attr('v-d1')
var com = Vue.extend({
template: dom.html()
})
return com
}
new Vue({
el: '#app',
data() {
return {
com: null
}
},
created() {
const html = document.getElementById('temp1').innerHTML
this.com = GetCom(html)
}
})
</script>
</body>
</html>
10 回答11.2k 阅读
5 回答4.8k 阅读✓ 已解决
4 回答3.1k 阅读✓ 已解决
2 回答2.7k 阅读✓ 已解决
2 回答4.8k 阅读✓ 已解决
4 回答4.3k 阅读✓ 已解决
4 回答1.9k 阅读✓ 已解决
并且在需要懒加载的图片上均按照如下使用
v-lazyload
指令即可