1

For example, this will throw a Vue error:

<img src="<?php echo get_stylesheet_directory_uri(); ?>/images/{{ form.screw.drive_image }}" >

I'm writing {{ form.screw.drive_image }} in the src prop.

It's not clear from Vue's warnings what I should do to solve.

Any idea?

4
  • what is the error warning? Commented Aug 20, 2018 at 13:23
  • A generic [Vue warn]: Error compiling template Commented Aug 20, 2018 at 13:24
  • 3
    :src="'<?=get_stylesheet_directory_uri(); ?>/images/' + form.screw.drive_image" > Commented Aug 20, 2018 at 13:30
  • @Derek that works thank you. Commented Aug 20, 2018 at 13:35

3 Answers 3

2

Thanks to Derek (see comments above), the correct answer is:

:src="'<?= get_stylesheet_directory_uri(); ?>/images/' + form.screw.drive_image"

Please note the followings:

  • :src is a shorthand for v-bind:src
  • <?= is a shorthand for <?php echo [...]
  • In this case Vue's data doesn't need braces
  • Computation in Vue's props is javascript, so in :src we are using + to concatenate
Sign up to request clarification or add additional context in comments.

1 Comment

Also reminder that Vue template {{...}} binding doesn't work in attributes, you need to use the : / v-bind: shortcut to denote an attribute that needs calculating/parsing vs a static string.
1

You would use the v-bind shorthand, from the documentation:

Dynamically bind one or more attributes, or a component prop to an expression.

The format:

<img :src="'some_string' + some_data">

The final product:

<img :src="'<?=get_stylesheet_directory_uri(); ?>/images/' + form.screw.drive_image">

Comments

0

You should use

<img v-bind:src="form.screw.drive_image" />

and change the value of form.screw.drive_image to include get_stylesheet_directory_uri()

Comments

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.