[英]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.