Sylvain C.
Sylvain C.

Reputation: 1073

Chain js function and then method in vue.js

Within a Vue component, I am calling a function from a separate JS file. I then need to call a method in my component just after this first function is completed:

My .vue component:

import myFunction from '@/components/functions';

export default {
  name: 'test',
  components: {
    myFunction,
  },      
  created(){
    if (....) {      
      myFunction.function1(myParam)          
        .then((response) => {
        this.method2();
       });         
  },  
  methods:{
    method2(){
      something;     
    },
  }
};

My separate functions.js file:

export default {
  function1(myParam) {
    ...
    return true;
  },
};

I tried several things such as the last one shown in my code which gives me a

.function1(...).then is not a function

I am sure it is not that complicated but can not find the correct syntax.

Upvotes: 0

Views: 947

Answers (1)

thezachcannon
thezachcannon

Reputation: 121

The function in your other file could return a Promise or it can except a callback from your view component. Also, if you set this equal to self/vm and then use vm.method2(), this is because in the then callback this is defined in scope of that function not the Vue component.

Upvotes: 1

Related Questions