0

When I use code below I'm getting error that istance this.message doesn't exist, but when I will push this as self to MyFunction(self), it will work good. So, the question is how can I push this to my JS file/function without using arguments of function from Vue?

import { MyFunction } from "./Functions/HelpFunctions.js";
data(){
   return{
       message: 'Hello World'
   }
}
methods: {
   MyFunction: MyFunction
},

created(){
   this.MyFunction();
}

JS file :

export const MyFunction  = () => {
    console.log(this.message); 
}

This cod works if I call it in Vue with pushing this:

export const MyFunction = (self) => {
    console.log(self.message); 
}
NoOneCare
  • 51
  • 5
  • Wouldn't it be `this.msg` since that's how you defined it in `data()`? – Phil Feb 21 '23 at 07:16
  • @Phil yes, but not in external JS script, which I imported – NoOneCare Feb 21 '23 at 07:17
  • So where exactly is `this.message` supposed to come from? No matter where it's defined `msg` is not `message` – Phil Feb 21 '23 at 07:18
  • @Phil I'm sorry, it's just a typo – NoOneCare Feb 21 '23 at 07:19
  • 1
    Your external functions should not have a dependency on `this` since that is only ever defined where and how the function is called. Instead, specify all the required parameters for the function, in this case a string `message`... `MyFunction () { MyFunction(this.message) }` – Phil Feb 21 '23 at 07:23

0 Answers0