[英]Error message. "Props with type Object/Array must use a factory function to return the default value."
I am using Vue-Cli3.0.我正在使用 Vue-Cli3.0。 I used this module for Vue.js.
我在 Vue.js 中使用了这个模块。 https://github.com/holiber/sl-vue-tree
https://github.com/holiber/sl-vue-tree
This is a customizable draggable tree component for Vue.js but I found that it could not copy functions of object.这是一个可自定义的 Vue.js 可拖动树组件,但我发现它无法复制对象的功能。
https://github.com/holiber/sl-vue-tree/blob/master/src/sl-vue-tree.js#L715 https://github.com/holiber/sl-vue-tree/blob/master/src/sl-vue-tree.js#L715
Because of here.因为这里。
JSON.parse(JSON.stringify(entity))
So I used this module and edited the copy function.所以我使用了这个模块并编辑了复制功能。
https://www.npmjs.com/package/clone https://www.npmjs.com/package/clone
var clone = require('clone');
copy(entity) {
return clone(entity)
},
In this way, the function of the object is correctly copied.这样,对象的功能就被正确复制了。
I already have tested it, and it worked correctly.我已经测试过了,它工作正常。 There was no problem with the performance but I got a console error.
性能没有问题,但出现控制台错误。
[Vue warn]: Invalid default value for prop "multiselectKey": Props with type Object/Array must use a factory function to return the default value.
found in
---> <SlVueTree>
I want to know the way to erase this error.我想知道消除这个错误的方法。 Thank you for reading my question.
感谢您阅读我的问题。
A factory function in props looks like this: props 中的工厂函数如下所示:
props: {
exampleDefaultObject: {
type: Object,
default() {
return {}
}
},
exampleDefaultArray: {
type: Array,
default() {
return []
}
}
},
or in ES6:或在 ES6 中:
props: {
exampleDefaultObject: {
type: Object,
default: () => ({})
},
exampleDefaultArray: {
type: Array,
default: () => []
}
},
(for people who come here looking for an explanation of the error in the question 'props with type object/array must use a factory function to return the default value') (对于来这里寻找问题“类型为对象/数组的道具必须使用工厂函数来返回默认值”中的错误的解释的人)
Note that when returning an object in an es6 arrow function, you need the parentheses: () => ({})
instead of () => {}
请注意,在 es6 箭头函数中返回对象时,需要括号:
() => ({})
而不是() => {}
according to your console warn, i find the error根据您的控制台警告,我发现了错误
https://github.com/holiber/sl-vue-tree/blob/master/src/sl-vue-tree.js#L30 https://github.com/holiber/sl-vue-tree/blob/master/src/sl-vue-tree.js#L30
try to fix it like this:尝试像这样修复它:
multiselectKey: {
type: [String, Array],
default: function () {
return ['ctrlKey', 'metaKey']
},
validator: function (value) {
let allowedKeys = ['ctrlKey', 'metaKey', 'altKey'];
let multiselectKeys = Array.isArray(value) ? value : [value];
multiselectKeys = multiselectKeys.filter(keyName => allowedKeys.indexOf(keyName ) !== -1);
return !!multiselectKeys.length;
}
},
the component default value must use a factory function to return!组件默认值必须使用工厂函数返回!
try it and hope it can help you试试看,希望它可以帮助你
these cases require a function than produce the default value, not an object or array:这些情况需要一个函数而不是产生默认值,而不是一个对象或数组:
obj_param:{
type:Object,
default:()=>{}
},
array_param:{
type:Array,
default:()=>[]
},
this is for performance reasons, since the function is called only if needed (no value so use default), while the direct value would be instantiated everytime the component is used这是出于性能原因,因为仅在需要时才调用该函数(没有值,因此使用默认值),而每次使用组件时都会实例化直接值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.