1

I'm creating a Vue application in which an expression will come from config as a variable. I need to run the expression (something like eval in javascript) and apply result to attribute

eg:

<v-text-field placeholder="{{expression}}"></v-text-field>

But this is showing the expression , not evaluating. Any way to achieve this?

Edit


Here expression can be something like

data.option=1?"ABC":"BCD"
3
  • Wait, why do you have 2 same accounts here? Commented Oct 16, 2022 at 21:00
  • I'm not having. I logged in on same from both mobile and laptop. I was unable to comment from my laptop so went back to mobile and tried. It's doing. Some stack overflow issue i doubt Commented Oct 16, 2022 at 21:04
  • 2
    You have 2 totally different accounts. Please use only one because it's forbidden and you may get one banned (maybe even both?). Commented Oct 16, 2022 at 21:13

2 Answers 2

2

You can try with eval but be aware::

const out = 'data.option === 1 ? "ABC" : "BCD"'
new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data() {
    return {
      data: {option: 1}
    }
  },
  computed: {
    expression() {
      return eval(JSON.parse(JSON.stringify('this.' + out)))
    }
  }
})
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/@mdi/[email protected]/css/materialdesignicons.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.css" rel="stylesheet">
<div id="app">
  <v-app>
    <v-main>
      <v-container>
        <v-text-field :placeholder="expression"></v-text-field>
      </v-container>
    </v-main>
  </v-app>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.js"></script>

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

5 Comments

Hi, Thanks for your answer. But here const out=data.option==1? 'ABC':'BCD' is the try. But it shows the text. Not evaluating the expression. How to achieve this is the query
@Binesh Nambiar C hey mate, check again pls, I updated my answer :)
Hi, Thanks for the answer again. But the expression needs to fit in a variable(I'm planning to load the expression from server response in future which will set by admin).
Thanks Nikola, I will try with this. To a level what i'm looking for is this. Two way binding etc... need to verify on this though. Thanks for your great support
Hi Nikola, It worked well as expected. I used var fn = new Function('dataObject', expr) return fn(this.dataObject) instead eval since mozilla site suggested don't use eval. But ideally the logic is very much same. Thanks for your great help again. Even binding etc all are behaving as expected
1

You can try to add colon before the property like following. To indicate that there is an expression in the middle of the quotation mark

<v-text-field :placeholder="expression"></v-text-field>

Comments

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.