-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
Do NOT throw warning when assigment in v-if
happens
#10184
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
Comments
Creating variables in a v-if is a hack. Use https://github.com/posva/vue-local-scope if you need to create local variables |
Thanks for your quick response. However, sadly, you closed this issue as quickly. And I don't quite understand what you mean by "hack". I presume you are saying it...
Let me discuss them. -- Optional binding is essentially a sugar syntax in Swift, for a specific but very meaningful purpose. Yes, people can always write something different and get similar results, but that does NOT mean the sugar syntax is unnecessary. Swift officially added this sugar syntax as a feature, for very good reasons. And I was suggesting the same thing to vue. "Creating local variables" is not the core of the problem. As I said, we have alternatives already. In fact, I'm using "baking data beforehand" for now. I can live with that. Developers can always live with such inconvenience. But I am requesting a better life with vue. Vue has evolved with several improvements which are not "indispensable" but ease a lot of pain. A simple example is I hope this good tendency continues. I've learnt your vue-local-scope before. This is about If I use vue-local-scope alongside The core of issue is:
Please kindly reevaluate this issue. |
I'd rather use |
Please read my words more thoroughly. There are reasons that sometimes I have data separated. Yes, I can bake data beforehand. In the example, I use This is how I do for now. I can live with that. |
It's a hack as in using a language pitfall as a feature: // this throws in strict mode
if (a = 3) doSomething() should be let a
if (a = 3) doSomething() For the same reason, you should declare the variable if you set it in a I hope this clears out things 🙂 |
Firstly, your code is not the same thing as optional binding. The correct counterpart in JS is: Either before ES6 without
Or with
Again, as I said, "optional binding" is essentially a "sugar syntax" But it is an official feature in Swift, which spares a lot of chores. Swift team decided to do so, instead of suggesting "well, you guys always have alternatives (like in many other old languages), so you should write long codes or make your own hacks (or borrow from others) and live with that!" Secondly, it was not pure JS that I was concerning. The vue has its own particular uses, and one of them makes "optional binding" very favorable. Surprisingly, vue already allows "optional binding", no matter purposely or not. I suspect not purposely. Maybe just like "Support destructuring assignment". Anyway, it is already so close for me to enjoy "optional binding" in vue, if there would not be the annoying "warning". |
Just now, when I prepared some data with
The cause here is: I have some paradigms and games. One paradigm has many games. Well, this makes circular structure between paradigms and games. But I hadn't anticipate that vue automatically calls |
That's not true: https://jsfiddle.net/Justineo/01ezbdyg/ |
Thanks, @Justineo . I don't know what was really going on, but I did encounter that error and my codes broke. I am sure it was vue who called And after I undid |
https://jsfiddle.net/Justineo/01ezbdyg/5/ And what you want is not an optional binding, instead, it's just an ordinary assignment expression, the “lexical binding” is not optional here. As Vue evaluates the compiled template code |
@Justineo Just now, I redid |
Sorry I didn't clarify that my intention was not for vue to apply "optional binding" exactly as Swift. The warning doesn't help anyone. It looks like an error, but the codes can run well despite it. It was not caused by a rule which forbids "assignment in v-if". There in no such a rule. And in issue #7631, @posva decided not add such a rule, which I totally agree. It was caused as you explained: vue cannot recognize the assignment and thus the new variable. But there's a fact that vue recognizes new variables in v-for. vue even supports "destructuring assignment", which is a little secret that doc doesn't metion, waiting for people to discover. So, I think it is very feasible for vue to recognize variables in v-if, and ignore them, instead of outputing those useless warnings. |
No it's not. Vue doesn't know whether |
Then it there ambiguity in Do you mean of a case like this ...? https://jsfiddle.net/69tq74xy/3/ Here, there's a It works as expected. Then, if I replace If I remove It works just like If a developer knows JS, and they should, they will understand this. Anyway, it is surely aweful to write codes like the example. No one will purposely write so. |
Currently |
You mean people may write codes like Yes, JS gives us a lot of freedom. Too much of it. I don't know whether Swift This issue is similar. My proposal here is:
All sugar syntaxes target particilar cases. If a case is comon enough, it is worth a sugar syntax, or better, a official feature. |
Even in the source code of vue.js itself, there are uses like |
v-if
happens
I've modified vue.js and successfully solved this issue. |
Done.
Remember to open Dev Tools and check the Console. |
See vuejs#10184 Especially, the last comment of mine, in which I gives links of two demos.
https://github.com/tc39/proposal-Declarations-in-Conditionals I suggest us keep an eye on this proposal and maybe this issue can be solved in JavaScript itself someday. |
Good to know. |
That's probably not gonna happen...it's only in Stage 1. |
What problem does this feature solve?
[UPDATE] I borrowed the idea "Optional Binding" from Swift which is famous. Unfortunately, this makes the topic astray... Please be noted that the real core here is "assigment in
v-if
".First off, what is "Optional Binding"?
Sounds fashionable. Yes, it is, in Swift.
In case you don't quite recall, below is the statement of this useful feature of Swift:
Which is also called "if let" statement.
And the doc is here: https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html#ID333
So, what's its counterpart in Vue?
For example:
I have data of a list of trainees and a dictionary of training records.
For some reason, the two are separated.
I have to check if a trainee has records, and if so, show details of the records.
You can see
training-records-by-trainee[trainee.id]
is used many times and the codes are terribly long and messy.This example is a bit extreme, but you can get the idea.
The good news is: we can assign a local variale in v-if!
This makes life much easier.
Alternatives?
Yes, I know, we can always use a component to handle
record-data
.But that would be overkill, unless the component can be reused.
Another way to ease the pain would be baking data beforehand.
For example,
Then we can write
trainee.record-data
.Neeter than
training-records-by-trainee[trainee.id]
, but not as neet as "optional binding".So, what's the problem?
This "optional binding" already works well and is as helpful just as in Swift.
Then, here's the bad news: it invites such a warning (actually, a red error):
This is an eyesore.
What does the proposed API look like?
Just make vue recognize optional binding and thus do not warn about the local variable, which IS "defined on the instance".
Possible controversy
Some people may dislike the idea of "allowing" or even "encouraging" assignment in
if
condition.An example is issue #7631: "warn when assignment in v-if in development mode"
As @posva said, "this is a js common pitfall for beginners" to mistakenly write "=" when they actually want "==".
And they usually would lean the lesson in hard way, having paid much for such mistakes.
I was one of them, of course. So I know how natural it is to have a reflexive aversion to "optional binding" when learning Swift.
But after a while, I realized we should not object to a very good thing only because of some other mistake, which is not its fault.
In pure js, even today, you can still make such a mistake like
if(a = b){console.log(a);}
, and you get a globalwindow.a
as an unexpected result.I hope someday "strict mode" will forbid this noxious pitfall and optional binding will be introduced into JavaScript, just like "for let" which already is in ES5.
Anyway, in vue, there is no worry about mistakenly creating a global variale via assignment in v-if.
No matter why, vue has the feature "optional binding" already, partially but effectively!
It is just unknown to the public. It is a hidden gem.
And that warning makes developers feel uneasy, although it is not a result of intention of guarding "=" in "v-if" at all.
The text was updated successfully, but these errors were encountered: