简体   繁体   中英

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. Thank you for your help

S.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM