1

$eval has been removed from Vue 2.

But, consider this JSFiddle (Vue 1): https://jsfiddle.net/kvdmolen/0w193c75

Here is JSFiddle with Vue 2.0.7: https://jsfiddle.net/kvdmolen/0w193c75/1/

Imagine this is actually a component (a table with configurable columns).

Any ideas how I should replace $eval for Vue 2?

Using eval() this doesn't work as it is outside of the v-for scope

6
  • Do you mean $eval in title as well? And what is the issue in the fiddle, it seems working fine to me. Commented Nov 18, 2016 at 10:20
  • Ah mistake, thanks! Commented Nov 18, 2016 at 10:21
  • The jsfiddle is using VueJS 1, not VueJS 2! Commented Nov 18, 2016 at 10:21
  • No, Js fiddle is using vue 2.0.5 Commented Nov 18, 2016 at 10:24
  • No, sorry, jsfiddle is messing up with the dropdown pre-selector. it was using Vue 1. Try this one: jsfiddle.net/kvdmolen/0w193c75 and then switch in the javascript dropdown Vue version Commented Nov 18, 2016 at 10:38

1 Answer 1

1

Instead of using eval, You can write the code by calling a function from HTML, and in the field you can write a function, which will determine what will be the output.

in Html:

        <tr v-for="(item, index) in myitems">
            <td v-for="column in mycolumns" v-text="myFunc(index, column.field)"> 
            </td>
        </tr>

in Vue Component:

    mycolumns: [
            {
                column: "Todo Name",
            field: (item) =>  item.name
        },
            {
                column: "Status",
            field: (item) =>  item.status
        },
            {
                column: "Status Explanation",
            field: (item) =>  item.status == 1 ? 'Done' : 'Todo'
        }
    ]
},
methods:{
   myFunc(index, fn){
     return fn(this.myitems[index])
   }
}

check working fiddle.

However this does not seem to be very good practice, as now your data is tightly bind to your html. So your container component and presentation container are not independent, can not grow independently and are error prone as well.

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

1 Comment

I think JSFiddle is not sweet with external resources. Consider this one: jsfiddle.net/kvdmolen/0w193c75/1 it has the built-in external resource and it does not work with Vue 2 (but works with Vue 1 if you change back to $eval)

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.