I have the following image definition.
Template:
<img :src="logoSVG" height="150px" alt="logo" />
JS:
data() {
return {
logoSVG: require('./assets/logo.svg')
}
}
This code works well with Vue 2. Note that the height of the image is set directly in the image.
Problem: After I upgraded to Vue 3, the images height is set to 0 in the rendered component.
Here is what it generates:
<img src="/img/logo.136099f1.svg" height="0" alt="logo">
Question: How to make Vue 3 correctly render height of an SVG image?
To get an element's height with Vue. js, we can assign a ref to the element we want to get the height for. Then we can get the height with the clientHeight property of the element that's been assigned the ref. We assigned the ref with the ref prop set to a name.
The thing to remember is that Vue 3 is backward-compatible (with some minor code changes). It doesn't cancel the existing way of doing things; instead, it adds new ways. If you are already using Vue 3, why not try it with Wijmo – a UI component library that is available in a Vue version.
To reset a component's initial data in Vue. js, we can create a function that returns the initial state object. to call the initialState function in data to return the object returned by initialState as the initial state values. Then in resetWindow , we call Object.
Try to remove px
from height="150px"
and keep it like :
<img :src="logoSVG" height="150" alt="logo" />
and the data property should be :
data() {
return {
logoSVG: require('./assets/logo.svg').default
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With