Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image height is reset to 0 after upgrading to Vue 3

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?

like image 501
Sasha Shpota Avatar asked Sep 28 '20 19:09

Sasha Shpota


People also ask

How do I get element height in Vue?

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.

Is Vue 3 backward compatible?

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.

How do I reset my Vue component data?

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.


1 Answers

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 
  }
}
like image 194
Boussadjra Brahim Avatar answered Oct 16 '22 22:10

Boussadjra Brahim