0

I imported some object from file in vue component. But when i am trying to using it via eval() ive got an error that references is not defined

import helper from './somehelperdir'

export default {
    methods: {
        somemethod () {
            console.log(helper) //everithing works great
            eval('helper') //helper is not defined
        }
    }
}

Helper returns

export default {
    simpleHelpermethod () {
        console.log('Very simple logic') 
    }
}
ItsMyLife
  • 458
  • 7
  • 21
  • 2
    You need a *really* good reason to use `eval()` - just saying... ([further reading](https://stackoverflow.com/questions/197769/when-is-javascripts-eval-not-evil)) – Mitya Jul 10 '18 at 13:19
  • @Utkanos i just think that `eval()` will be looking better then a lot of if : else(switch case) – ItsMyLife Jul 10 '18 at 13:21
  • What does helper return? – A. Llorente Jul 10 '18 at 13:24
  • @A.Llorente there is simplest logic. Updated in question – ItsMyLife Jul 10 '18 at 13:26
  • 2
    Why don't you just execute the function then? – A. Llorente Jul 10 '18 at 13:29
  • @A.Llorente i have a lot of helpers with different ends like helperOne, helperTwo etc. And I dont want to use huge switch case constructions – ItsMyLife Jul 10 '18 at 13:33
  • 2
    You can encapsulate switch constructions using objects: https://toddmotto.com/deprecating-the-switch-statement-for-object-literals/ – A. Llorente Jul 10 '18 at 13:36
  • @A.Llorente anyway in this case i should to call function as a value of object. And the only way to do this is execute this via `eval()` – ItsMyLife Jul 10 '18 at 13:40
  • I'm not sure what you're trying to achieve but why don't you just use helper() ? Or maybe export an object and import in the component only what you need. import { method1, method2 } from './helpers' and then something like method1() – lucas Jul 10 '18 at 14:28

0 Answers0