【问题标题】:Vue.js, Functions inside methods are not found [duplicate]Vue.js,未找到方法内的函数[重复]
【发布时间】:2019-03-13 15:08:27
【问题描述】:

我刚开始使用 Vue.js,现在在使用生命周期方法中的函数时遇到问题。

<script>
export default {
  name: 'MapComponent',
  data () {
    return {
      restaurants: [],
      menus: []
    }
  },
  mounted: () => {
    this.augmented(2)
  },
  methods: {
    augmented: function (variable) {
      return (2 * variable)
    }
  }
}
</script>

我的代码如上所示。问题是,当我调用函数增强时,我得到一个错误,即“挂载钩子中的错误:“TypeError:_this.augmented 不是函数”

有人能解释一下为什么没有找到增强的功能吗?

感谢您的帮助。

【问题讨论】:

    标签: javascript vue.js vuejs2 vue-component


    【解决方案1】:

    这里不要使用箭头函数,因为箭头函数会为 Vue 中的“this”关键字分配不同的上下文。

    mounted: function() { this.augmented(2) };
    

    【讨论】:

    • 谢谢你解决了我的问题。
    • 浪费了 2 小时...我有一个函数在方法中调用另一个 Vue func{}。
    猜你喜欢
    • 2018-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-16
    • 2021-12-17
    • 1970-01-01
    • 2017-05-22
    • 1970-01-01
    相关资源
    最近更新 更多