简体   繁体   English

如何从另一个组件调用 mixin function?

[英]How do I call a mixin function from another component?

MyMixin.vue has the method beginEdit . MyMixin.vue有方法beginEdit

What I'm trying to do is to make onFirstLineClick to call myMixin's beginEdit depending on the value of this.folded .我要做的是让onFirstLineClick根据this.folded beginEdit

When I console logged myMixin.beginEdit , it is undefined and not surprisingly myMixin.beginEdit() doesn't work.当我在控制台登录myMixin.beginEdit时,它是undefined的,毫不奇怪myMixin.beginEdit()不起作用。

Am I missing something needed to use the function?我是否缺少使用 function 所需的东西? If so, why does beginEdit work perfectly on <span> ?如果是这样,为什么beginEdit<span>上完美运行?

<template>
  <div>
    <div>
      <div
        @click="onFirstLineClick"
      />
      <span
        @click="beginEdit"
      />
  </div>
</template>

<script>
import myMixin from './MyMixin';

export default {
  name: 'currComponent',
  mixins: [myMixin],
  data() {
    return {
      folded: false,
    };
  },
  methods: {
    onFirstLineClick(e) {
      // myMixin.beginEdit() doesn't work
    }
  },
};
</script>

The great thing about mixin is that when a component uses a mixin, all options in the mixin will be "mixed" into the component's own options. mixin的伟大之处在于,当组件使用 mixin 时,mixin 中的所有选项都会“混合”到组件自己的选项中。 Which means that inside your component you can call mixin method directly like:这意味着在您的组件内部,您可以直接调用mixin方法,例如:

methods: {
  onFirstLineClick(e) {
    this.beginEdit()
  }
},

That is also the reason why you can use beginEdit() method on <span> directly like:这也是您可以直接在<span>上使用beginEdit()方法的原因,例如:

<span @click="beginEdit" />

Please also know that in future if you declare a method in this component with same name as mixin method name beginEdit , then the component's method will take priority and you might see different behaviour.另请注意,将来如果您在此组件中声明一个与mixin方法名称beginEdit的方法,则该组件的方法将优先,您可能会看到不同的行为。 So, make sure to give unique names to mixin methods.因此,请确保为mixin方法提供唯一的名称。

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

相关问题 如何从同一对象的另一个mixin中调用在一个CoffeeScript mixin中定义的方法? - How can I call a method defined in one CoffeeScript mixin from another mixin on the same object? 我如何从另一个不是反应组件的文件中调用 useReducer 调度 function? (不使用 Redux) - How do i call a useReducer dispatch function from another file, which is not a react component? (without using Redux) 如何在 React Native 中从另一个组件调用/执行函数? - How do I call/execute a function from another component in react native? 我如何从另一个组件调用 function 以响应本机? - How would I call a function from another component in react native? 如何将 function 从一个组件调用到另一个组件? 反应 - How can I call a function from one component to another? React 如何从另一个组件调用 function 做出反应 - How can I call a function from another component react 如何从既不是子项也不是父项的组件调用函数? - How do I call a function from component that is not a child nor parent? 如何从其他组件调用默认函数 - How do i call default function from different component 如何从React中的另一个组件调用函数 - how to call a function from another component in react 如何从另一个函数返回一个函数然后调用它? - How do I return a function from another function and then call it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM