mirror of https://github.com/ElemeFE/element
Image: add support for transmit "attrs" and "listeners" (#15578)
parent
c4958488cb
commit
855062423c
|
@ -9,9 +9,9 @@
|
||||||
<img
|
<img
|
||||||
v-else
|
v-else
|
||||||
class="el-image__inner"
|
class="el-image__inner"
|
||||||
|
v-bind="$attrs"
|
||||||
|
v-on="$listeners"
|
||||||
:src="src"
|
:src="src"
|
||||||
:alt="alt"
|
|
||||||
:referrerpolicy="referrerPolicy"
|
|
||||||
:style="imageStyle"
|
:style="imageStyle"
|
||||||
:class="{ 'el-image__inner--center': alignCenter }">
|
:class="{ 'el-image__inner--center': alignCenter }">
|
||||||
</div>
|
</div>
|
||||||
|
@ -37,14 +37,13 @@
|
||||||
name: 'ElImage',
|
name: 'ElImage',
|
||||||
|
|
||||||
mixins: [Locale],
|
mixins: [Locale],
|
||||||
|
inheritAttrs: false,
|
||||||
|
|
||||||
props: {
|
props: {
|
||||||
src: String,
|
src: String,
|
||||||
fit: String,
|
fit: String,
|
||||||
lazy: Boolean,
|
lazy: Boolean,
|
||||||
scrollContainer: {},
|
scrollContainer: {}
|
||||||
alt: String,
|
|
||||||
referrerPolicy: String
|
|
||||||
},
|
},
|
||||||
|
|
||||||
data() {
|
data() {
|
||||||
|
@ -104,13 +103,20 @@
|
||||||
const img = new Image();
|
const img = new Image();
|
||||||
img.onload = e => this.handleLoad(e, img);
|
img.onload = e => this.handleLoad(e, img);
|
||||||
img.onerror = this.handleError.bind(this);
|
img.onerror = this.handleError.bind(this);
|
||||||
|
|
||||||
|
// bind html attrs
|
||||||
|
// so it can behave consistently
|
||||||
|
Object.keys(this.$attrs)
|
||||||
|
.forEach((key) => {
|
||||||
|
const value = this.$attrs[key];
|
||||||
|
img.setAttribute(key, value);
|
||||||
|
});
|
||||||
img.src = this.src;
|
img.src = this.src;
|
||||||
},
|
},
|
||||||
handleLoad(e, img) {
|
handleLoad(e, img) {
|
||||||
this.imageWidth = img.width;
|
this.imageWidth = img.width;
|
||||||
this.imageHeight = img.height;
|
this.imageHeight = img.height;
|
||||||
this.loading = false;
|
this.loading = false;
|
||||||
this.$emit('load', e);
|
|
||||||
},
|
},
|
||||||
handleError(e) {
|
handleError(e) {
|
||||||
this.loading = false;
|
this.loading = false;
|
||||||
|
|
|
@ -64,5 +64,49 @@ describe('Image', () => {
|
||||||
await wait();
|
await wait();
|
||||||
expect(image2.loading).to.be.false;
|
expect(image2.loading).to.be.false;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('$attrs', async() => {
|
||||||
|
vm = createVue({
|
||||||
|
template: `
|
||||||
|
<el-image
|
||||||
|
alt="$attrs test"
|
||||||
|
referrerpolicy="origin"
|
||||||
|
:src="src"></el-image>
|
||||||
|
`,
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
src
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}, true);
|
||||||
|
|
||||||
|
await wait();
|
||||||
|
const $img = vm.$el.querySelector('.el-image__inner');
|
||||||
|
expect($img.getAttribute('alt')).to.be.equal('$attrs test');
|
||||||
|
expect($img.getAttribute('referrerpolicy')).to.be.equal('origin');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('pass event listeners', async() => {
|
||||||
|
let result;
|
||||||
|
vm = createVue({
|
||||||
|
template: `
|
||||||
|
<el-image @click="handleClick" :src="src"></el-image>
|
||||||
|
`,
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
src
|
||||||
|
};
|
||||||
|
},
|
||||||
|
methods: {
|
||||||
|
handleClick(e) {
|
||||||
|
result = e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, true);
|
||||||
|
await wait();
|
||||||
|
vm.$el.querySelector('.el-image__inner').click();
|
||||||
|
await wait();
|
||||||
|
expect(result).to.exist;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue