question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Optional chaining in templates does not seem to work

See original GitHub issue

Version

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:closed
  • Created 4 years ago
  • Reactions:274
  • Comments:63 (6 by maintainers)

github_iconTop GitHub Comments

86reactions
McPocommented, May 15, 2020

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.

Vue.mixin({
  methods: {
    $elvis: p => eval('this.'+p)
  }
});

Example template

<template>
<span>{{ $elvis('foo?.bar') }}</span>
</template>

Although its still no substitute for the real operator, especially if you have many occurrences of it

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found