[英]Use current vue component's method as default prop value
I am going to pass function as property to Vue component.我将把函数作为属性传递给 Vue 组件。 Let it be function has to be invoked by
@click
.让它成为函数必须由
@click
调用。 But for some reasons I want to keep default behaviour for the component.但由于某些原因,我想保留组件的默认行为。 The default behaviour is not so easy as I want and I'm going to use
method
as default function.默认行为并不像我想要的那么简单,我将使用
method
作为默认函数。 Because the default behaviour requires data from component's state (props, data, other methods, whatever).因为默认行为需要来自组件状态的数据(道具、数据、其他方法等)。
Is there a way to do?有办法吗?
I've attached the example.我附上了这个例子。 Expected behaviour:
预期行为:
Button works fine
should produce alert You are welcome!
按钮
works fine
应该产生警报You are welcome!
Button nope
should produce alert You are welcome as well!
按钮
nope
应该产生警报You are welcome as well!
but does nothing.但什么也不做。
Vue.component('welcome-component', { template: '#welcome-component', props: { name: { type: String, required: true, }, action: { type: Function, required: false, default: () => { this.innerMethod() }, }, }, methods: { innerMethod() { alert("You are welcome as well!"); } } }); var app = new Vue({ el: "#app", methods: { externalMethod() { alert("You are welcome!"); } } });
#app { margin: 20px; } .holder > button { margin: 10px; padding: 5px; font-size: 1.1em; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <welcome-component name="works fine" :action="externalMethod"></welcome-component> <welcome-component name="nope"></welcome-component> </div> <script id='welcome-component' type='x-template'> <div class="holder"> <button @click="action">{{ name }}</button> </div> </script>
From vue docs:来自 vue 文档:
"Note that props are validated before a component instance is created, so instance properties (eg data, computed, etc) will not be available inside default or validator functions."
( https://v2.vuejs.org/v2/guide/components-props.html ) ( https://v2.vuejs.org/v2/guide/components-props.html )
Referencing the innerMethod
is therefore one of the cases where the components function is not yet available.因此,引用
innerMethod
是组件功能尚不可用的情况之一。
Idea: If it is crucial for you to have this kind of functionality you could check within a later lifecycle hook (like created, mounted etc.) whether the type of action
is function
.想法:如果拥有这种功能对您来说至关重要,您可以在以后的生命周期钩子(如 created、mounted 等)中检查
action
类型是否为function
。 If it's not a function (meaning not passed via prop), assign the innerMethod manually.如果它不是函数(意味着不通过 prop 传递),请手动分配 innerMethod。
This works but it's a total bag of spanners:这行得通,但它是一包扳手:
action: {
required: false,
default () {
return () => this.innerMethod();
},
},
I've had to remove the type: Function
.我不得不删除
type: Function
。 Usually when default
is a function it will be invoked to return the appropriate default value.通常当
default
是一个函数时,它将被调用以返回适当的默认值。 However, if the prop has type: Function
it'll just treat the function as the default.但是,如果 prop 具有
type: Function
,它只会将该函数视为默认值。 In this case that's problematic because we lose the this
binding.在这种情况下,这是有问题的,因为我们失去了
this
绑定。
The internal arrow function is required to get around the problem that the methods aren't available when the default
function is called.需要内部箭头函数来解决调用
default
函数时方法不可用的问题。
If possible I would suggest giving up on using a default
and instead just apply the 'default' when it needs to be invoked.如果可能的话,我建议放弃使用
default
,而是在需要调用时应用“默认值”。 So rather than calling action
directly in the click
handler, instead call a method called invokeAction
that looks something like this:因此,与其直接在
click
处理程序中调用action
,不如调用一个名为invokeAction
的方法,如下所示:
invokeAction () {
if (this.action) {
this.action();
} else {
this.innerMethod();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.