diff --git a/src/image/image.ts b/src/image/image.ts index ce5f90ff8..5d11e2a55 100644 --- a/src/image/image.ts +++ b/src/image/image.ts @@ -28,18 +28,9 @@ export default class Image extends SuperComponent { lifetimes = { attached() { const { width, height } = this.data; - let innerStyle = ''; - this.update(); - if (width) { - innerStyle += `width: ${addUnit(width)};`; - } - if (height) { - innerStyle += `height: ${addUnit(height)};`; - } - this.setData({ - innerStyle, - }); + this.update(); + this.calcSize(width, height); }, }; @@ -48,6 +39,9 @@ export default class Image extends SuperComponent { if (this.preSrc === this.properties.src) return; this.update(); }, + 'width, height'(width, height) { + this.calcSize(width, height); + }, }; methods = { @@ -84,6 +78,20 @@ export default class Image extends SuperComponent { this.triggerEvent('error', e.detail); }, + calcSize(width, height) { + let innerStyle = ''; + + if (width) { + innerStyle += `width: ${addUnit(width)};`; + } + if (height) { + innerStyle += `height: ${addUnit(height)};`; + } + this.setData({ + innerStyle, + }); + }, + update() { const { src } = this.properties; this.preSrc = src;