【问题标题】:Chain js function and then method in vue.jsvue.js 中的链式 js 函数和 then 方法
【发布时间】:2018-09-12 04:39:44
【问题描述】:

在一个 Vue 组件中,我从一个单独的 js 文件中调用一个函数。 然后我需要在第一个函数完成后在我的组件中调用一个方法:

我的 .vue 组件:

import myFunction from '@/components/functions';

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

我单独的functions.js文件:

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

我尝试了几件事,例如我的代码中显示的最后一个,它给了我一个

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

我确信它没有那么复杂,但找不到正确的语法。 谢谢你的帮助

S.

【问题讨论】:

    标签: javascript vue.js ecmascript-6


    【解决方案1】:

    您的其他文件中的函数可以返回一个 Promise,也可以返回来自您的视图组件的回调。另外,如果将 this 设置为 self/vm,然后使用 vm.method2(),这是因为在 then 回调中 this 是在该函数而不是 Vue 组件的范围内定义的。

    【讨论】:

      猜你喜欢
      • 2020-10-22
      • 2017-11-03
      • 2016-02-14
      • 2021-08-04
      • 1970-01-01
      • 2019-11-06
      • 1970-01-01
      • 2014-10-12
      • 2020-04-05
      相关资源
      最近更新 更多