9

My code is like this :

<multiple-photo-product :product="{{ isset($product) ? $product : '' }}"></multiple-photo-product>

When the code runs it throws an error:

SyntaxError: Unexpected token } in

But if the code is like this:

 <multiple-photo-product product="{{ isset($product) ? $product : '' }}"></multiple-photo-product>

It doesn't throw an error.

I add :, so that the data is sent as an object.

If it does not use :, the data is sent as a string.

How can I solve it?

10
  • 1
    Is isset($product) client side code or server side code? Commented Jul 10, 2017 at 4:53
  • @Bert Evans, server side code Commented Jul 10, 2017 at 5:06
  • Is the error server side or client side? If it's client side, what is actually rendered as a result of your ternary? Commented Jul 10, 2017 at 5:11
  • 3
    I do not think you are allowed to use interpolation inside of bindings. Try :product="isset($product) ? $product : '' " Commented Jul 10, 2017 at 5:27
  • 1
    Can you add the rendered HTML including the rendered object to your question please? Commented Jul 10, 2017 at 14:55

2 Answers 2

7

The problem lies in the fact that if the php variable $product is not set (i.e equals to null or ""), then Vue will try to bind the prop :product with '' which ultimately results to an error (like trying to make a :product="" bind)

Try the following:

<multiple-photo-product :product="{{ isset($product) ? $product : '""' }}"></multiple-photo-product>

Notice the double quotes "" surrounded by the single quotes. This will say to Vue to bind the product prop with an empty string, in case php's $product variable is not set.

Please also have a look here. You may find it helpful. The key point to recall is that v-bind expects valid javascript expressions, that is the interpolated value (i.e. whatever is inside the Blade's curly braces {{}}) must be a valid javascript expression too

Sign up to request clarification or add additional context in comments.

Comments

-3

In VueJS2, the attribute properties with : don't need the {{ }}. In your case, the ternary is like that :

<multiple-photo-product product="isset($product) ? $product : ''"></multiple-photo-product>

Source : https://v2.vuejs.org/v2/guide/syntax.html#Attributes

Mustaches cannot be used inside HTML attributes, instead use a v-bind directive

1 Comment

The {{}} is a server side interpolation. He is using laravel.

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.