【问题标题】:I can't call a mixin function inside a javascript function我不能在 javascript 函数中调用 mixin 函数
【发布时间】:2020-04-24 06:38:53
【问题描述】:

我正在使用 nuxt.js 开发一个小项目,现在我已经使用 mixin 创建了一个插件,如您所见,但我不知道为什么函数 b 在渲染函数中不起作用:

import Vue from 'vue'


Vue.mixin({
  methods: {

    a () {
      const render = function () {
        this.b()
      }
      render()
    },

    b () {
      alert('testme')
    }

  }
})

【问题讨论】:

  • 在您的渲染函数中 - 您确定“this”是您所期望的吗?
  • 我只想调用 b 函数我不知道怎么做:(
  • 使用箭头函数定义render

标签: javascript vue.js nuxt.js vue-mixin


【解决方案1】:

由于您使用function 关键字来定义render,因此其中的this 指的是调用上下文(a),它没有属性b。

解决方法是使用箭头函数:const render = () => this.b();

const methods = {

  a() {
    const render = () => {
      this.b()
    }
    render()
  },

  b() {
    alert('testme')
  }

}

methods.a()

【讨论】:

    猜你喜欢
    • 2018-10-27
    • 1970-01-01
    • 2022-01-15
    • 2014-04-29
    • 2012-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多