简体   繁体   English

Vue:prop 函数的默认值

[英]Vue: default value for prop function

Is there any way to set default function for prop with function type?有没有办法为具有函数类型的道具设置默认函数?

props: {
    clickFunction: {
        type: 'Function'
        default: ????
    }
}

Yes:是的:

 Vue.component('foo', { template: '<div>{{ num }}</div>', props: { func: { type: Function, default: () => 1, }, }, data() { return { num: this.func() } } }) new Vue({ el: '#app', });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <foo></foo> </div>

I'd say, always provide a default value that matched the type you specified: makes things much more easier to reason about.我想说,始终提供与您指定的类型相匹配的默认值:让事情更容易推理。 Here, what you are looking for would be a noop function (aka a function that does nothing):在这里,您正在寻找的是一个 noop 函数(也就是一个什么都不做的函数):

props: {
  clickFunction: {
    type: Function
    default: () => {}
  }
}

You can then use this.clickFunction() in your code without checking it defensively first: it is always a function.然后,您可以在代码中使用this.clickFunction()而无需先进行防御性检查:它始终是一个函数。 Be sure you don't get mixed up with the default value for an Object prop, that would be an empty object:确保不要混淆Object道具的默认值,这将是一个空对象:

props: {
  someObject: {
    type: Object
    default: () => ({})
  }
}

In Vue, Object and Array props must have their default value returned by a function to ensure they are distinct for every instance of the component (which does not seem to be required for Function props, but docs indeed aren't crystal clear about this).在 Vue 中, ObjectArray props 必须由函数返回其默认值,以确保它们对于组件的每个实例都是不同的(这似乎不是Function props 所必需的,但文档确实对此并不十分清楚) .

This way should work with Vue 2.0这种方式应该适用于 Vue 2.0

props: {
    clickFunction: {
        type: 'Function'
        default(){
          return () => true
        }
    }
}

Is there any way to set default function for prop with function type?有什么方法可以为具有函数类型的 prop 设置默认函数吗?

props: {
    clickFunction: {
        type: 'Function'
        default: ????
    }
}

I think this is not the right approach.我认为这不是正确的做法。 VUE do not work that way. VUE 不是这样工作的。 Why do want to pass reference to function as props?为什么要传递对函数的引用作为道具? Component can emit its own event that parent can catch.组件可以发出自己的事件,父可以捕获。

Here you have available props types: https://v2.vuejs.org/v2/guide/components-props.html这里有可用的道具类型: https ://v2.vuejs.org/v2/guide/components-props.html

And here you can read about cutom events: https://v2.vuejs.org/v2/guide/components-custom-events.html#Event-Names在这里您可以阅读有关自定义事件的信息: https ://v2.vuejs.org/v2/guide/components-custom-events.html#Event-Names

Edit Yeah, my answer is not correct and does not answer the question.编辑是的,我的回答不正确,也没有回答问题。 Straight answer: yes, there are some any ways to set default function for prop with function type (look other answers).直接回答:是的,有一些方法可以为具有函数类型的道具设置默认函数(查看其他答案)。

Patterns and use-cases are off-topic so I end here (however, someone mentioned an interesting thing in response to my answer).模式和用例是题外话,所以我到此结束(但是,有人在回答我的回答时提到了一件有趣的事情)。

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

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