简体   繁体   English

我无法在 javascript 函数中调用 mixin 函数

[英]I can't call a mixin function inside a javascript function

i'm working on a small project using nuxt.js and right now i have created a plugin using mixin as you can see, but i don't know why function b doesn't work inside render function :我正在使用 nuxt.js 开发一个小项目,现在我已经使用 mixin 创建了一个插件,如您所见,但我不知道为什么函数 b 在渲染函数中不起作用:

import Vue from 'vue'


Vue.mixin({
  methods: {

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

    b () {
      alert('testme')
    }

  }
})

Since you used the function keyword to define render the this therein refers to the calling context ( a ) which has no property b.由于您使用function关键字来定义renderthis其中的this指的是没有属性 b 的调用上下文 ( a )。

The solution is to use an arrow function: const render = () => this.b();解决方法是使用箭头函数: const render = () => this.b(); . .

 const methods = { a() { const render = () => { this.b() } render() }, b() { alert('testme') } } methods.a()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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