从另一个函数调用时函数未定义错误

时间:2018-04-10 18:02:47

标签: javascript vue.js

所以我从另一个函数中调用一个函数:

methods: {
  selectStep: function (id) {
    ...
  }

  controlStep: function () {
    selectStep(1);
    ...
  }
}

但我得到的只是一个错误说:

Uncaught ReferenceError: selectStep is not defined

你对这里发生的事情有什么想法吗?

2 个答案:

答案 0 :(得分:2)

  

你必须使用this.anotherMethodName

<template>
    ...

    <button @click="callMethod"></button>

    ...
</template>

<script>
    export default {
        methods: {
            firstMethod() {
                console.log('called')
            },
            callMethod() {
                this.firstMethod()
            }
        }
    }
</script>

答案 1 :(得分:2)

您的方法是尝试从window对象或从对象methods声明的任何上下文执行函数。

您需要按以下方式调用该功能:

this.selecteStep(1);

此外,您需要使用逗号,

分隔这些方法/函数

&#13;
&#13;
const methods = {
  selectStep: function (id) {
    console.log('called!', id);
  },
  controlStep: function () {
    this.selectStep(1);
  }
}

methods.controlStep();
&#13;
&#13;
&#13;

相关问题