2

I have a Svg and would like to make some parts of the viewbox attribute dynamic.

svg :viewBox="0 0 {{x}} {{y}}" :width="x" :height="y">

</svg>

What is the right syntax to implement this

2 Answers 2

4

You can't use interpolation, you will need a computed:

computed:{
  viewbox(){
    return "0 0 " + this.x + " " + this.y;
  }
}

Then in your markup:

<svg :viewBox="viewbox" :width="x" :height="y"></svg>
Sign up to request clarification or add additional context in comments.

2 Comments

You seem to be pretty luke warm about computeds, but that's standard in Vue and every other MVVM framework. You can of course write it in line, but that's just nasty: :viewBox="'0 0 '+ x + ' ' y"
Not sure what has changed since, but the interpolation or how the value is generated doesn't seem to be what's causing the camelCase attribute to be lowercased: codepen.io/perpetual-education/pen/ExdLWEJ
2

The accepted solution does not work appropriately due to how svg handles viewBox.

You need to bind it as follows: <svg :view-box.camel="viewbox" :width="x" :height="y"></svg>

With the other answer Vue converts the camel case of viewBox to all lowercase and the component does not render correctly. This will keep the correct camel case on viewBox

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.