Optional chaining in templates does not seem to work
See original GitHub issueVersion
15.8.3
Reproduction link
https://template-explorer.vuejs.org/#<div id%3D"app" v-if%3D"obj%3F.a">{{ msg }}<%2Fdiv>
Steps to reproduce
Use a v-if that uses optional chaining w/ @vue/cli version 4.2.0
:
v-if="test?.length > 0"
What is expected?
no error is thrown
What is actually happening?
following error is thrown:
Errors compiling template:
invalid expression: Unexpected token '.' in
test?.length > 0
Raw expression: v-if="test?.length > 0"
Issue Analytics
- State:
- Created 4 years ago
- Reactions:274
- Comments:63 (6 by maintainers)
Top Results From Across the Web
Support optional chaining in vuejs
I have created vue and electron app using @vue/cli-service 4.2 in that I am facing a issue of optional chaining. I can't use...
Read more >Optional chaining (?.) - JavaScript - MDN Web Docs - Mozilla
The optional chaining (?.) operator accesses an object's property or calls a function. If the object accessed or function called is ...
Read more >Why are template strings not allowed in optional chaining?
There's a note explaining the purpose is to maintain consistency with code without optional chaining. String.raw`\t`; // works String?.raw`\t` // syntax error ...
Read more >Optional chaining gotcha in Angular | Dai Codes: A Blog
The optional chaining operator behaves differently in Angular templates ... in the progress component, and the notStarted template was not.
Read more >What is wrong with optional chaining and how to fix it
In situation where z equals 0, this code would not add 1, as if is working by previous rules, so 0 is considered...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Potential workaround includes using lodash’s get as stated in https://github.com/vuejs/vue/issues/4638#issuecomment-397770996
Another hack is to use
eval
.As
$eval
was taken out in Vue 2, you’ll have to create your own mixin so that it can be accessed in all components without having to import it into each one.i.e.
Example template
Although its still no substitute for the real operator, especially if you have many occurrences of it
As said in the release notes:
https://vue-next-template-explorer.netlify.com/#{"src"%3A"<div id%3D\"app\" v-if%3D\"obj%3F.a\">{{ msg }}<%2Fdiv>"%2C"options"%3A{"mode"%3A"module"%2C"prefixIdentifiers"%3Afalse%2C"hoistStatic"%3Afalse%2C"cacheHandlers"%3Afalse%2C"scopeId"%3Anull}}