Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compilation fails on empty values on v-bind #7973

Closed
stephan-v opened this issue Apr 6, 2018 · 10 comments
Closed

Compilation fails on empty values on v-bind #7973

stephan-v opened this issue Apr 6, 2018 · 10 comments

Comments

@stephan-v
Copy link

Version

2.5.13

Reproduction link

https://jsfiddle.net/jt3hjuwg/

Steps to reproduce

I am referencing this closed issue:

#3969

I feel like something should be done about this issue because it has left me with some white pages along the way and without a clear error message. If you pass in a null value as a prop the entire compilation breaks down with your evaluated html simply being dumped into the console.

Is there really not a better way to handle this? Obviously a user should or could check for these values before actually showing the component but in large application a piece of data can sometimes be null either by accident or oversight

If nothing can be done about this could someone at least please explain why this happens because in my eyes a component not getting a prop does not justify the entire compilation failing.

What is expected?

The application should not fail compilation.

What is actually happening?

It fails compilation.

@kidplug
Copy link

kidplug commented Apr 6, 2018

You're not passing a null value as the :content - you're passing nothing.

To properly pass a JS null it should be:
:content="null"

Remember this is a "bound" attribute - the text inside the quotes is not used literally - its used as a JS expression, evaluated against the data/scope of the vue instance.

@posva
Copy link
Member

posva commented Apr 6, 2018

Maybe a warning can be added in dev mode

@stephan-v
Copy link
Author

stephan-v commented Apr 7, 2018

@kidplug

In the example given you are correct, but what if I were to pass along a null value from something like PHP though? It still fails, it acts as If I have not given it anything at all and the entire compilation breaks.

<?php $nullValue = null; ?>

<review :content="<?= $nullValue ?>"></review>

This is the bothersome part right here, it is that I am actually supplying a null value.

@posva posva changed the title Compilation fails on null values Compilation fails on empty values Apr 7, 2018
@posva posva changed the title Compilation fails on empty values Compilation fails on empty values on v-bind Apr 7, 2018
@jacekkarczmarczyk
Copy link

You should do

<?php $value = whatever; ?>

<review :content="<?= json_encode($value) ?>"></review>

@yyx990803
Copy link
Member

yyx990803 commented Apr 7, 2018

Note this is supposed to fail, because by definition, using v-bind means the content is treated as JavaScript expression, so it's the equivalent of writing the following in JavaScript:

createElement('review', { attrs: { content: }})

What we can do is giving a proper compilation warning instead of a runtime error. This can be a good first contribution.

@lyhper
Copy link
Contributor

lyhper commented Apr 9, 2018

I'd like to have a try

lyhper added a commit to lyhper/vue that referenced this issue Apr 9, 2018
lyhper added a commit to lyhper/vue that referenced this issue Apr 9, 2018
@leevigraham
Copy link

Related: #3969

@sodatea sodatea added the has PR label Jun 4, 2018
@stephan-v
Copy link
Author

By the way what is being suggested here by @jacekkarczmarczyk will not work for numbers since they are being converted to strings this way.

<?php $value = whatever; ?>

<review :content="<?= json_encode($value) ?>"></review>

@jacekkarczmarczyk
Copy link

No, numbers will be numbers, if you have strings it means than you have strings instead of numbers
image

yyx990803 pushed a commit that referenced this issue Oct 24, 2018
* refactor: add empty v-bind warnings

re #7973

* Update index.js
@yyx990803
Copy link
Member

closed via #7940

f2009 pushed a commit to f2009/vue that referenced this issue Jan 25, 2019
* refactor: add empty v-bind warnings

re vuejs#7973

* Update index.js
aJean pushed a commit to aJean/vue that referenced this issue Aug 19, 2020
* refactor: add empty v-bind warnings

re vuejs#7973

* Update index.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants