Javascript 错误消息&引用;类型为Object/Array的道具必须使用factory函数返回默认值;
我正在使用Vue-Cli3.0。我将此模块用于Vue.js 这是Vue.js的一个可定制的可拖动树组件,但我发现它无法复制对象的函数 因为这里Javascript 错误消息&引用;类型为Object/Array的道具必须使用factory函数返回默认值;,javascript,typescript,vue.js,vuejs2,Javascript,Typescript,Vue.js,Vuejs2,我正在使用Vue-Cli3.0。我将此模块用于Vue.js 这是Vue.js的一个可定制的可拖动树组件,但我发现它无法复制对象的函数 因为这里 JSON.parse(JSON.stringify(entity)) 所以我使用了这个模块并编辑了复制功能 这样,就可以正确复制对象的功能 我已经测试过了,它工作正常。性能没有问题,但我遇到了一个控制台错误 [Vue warn]: Invalid default value for prop "multiselectKey": Props with
JSON.parse(JSON.stringify(entity))
所以我使用了这个模块并编辑了复制功能
这样,就可以正确复制对象的功能
我已经测试过了,它工作正常。性能没有问题,但我遇到了一个控制台错误
[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>
[Vue warn]:道具“multiselectKey”的默认值无效:类型为Object/Array的道具必须使用工厂函数返回默认值。
发现于
--->
我想知道如何消除这个错误。
感谢您阅读我的问题。根据您的控制台警告,我发现了错误 试着像这样修复它:
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;
}
},
props: {
exampleDefaultObject: {
type: Object,
default() {
return {}
}
},
exampleDefaultArray: {
type: Array,
default() {
return []
}
}
},
组件默认值必须使用工厂函数才能返回
尝试一下,希望它能帮助您道具中的工厂功能如下所示:
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;
}
},
props: {
exampleDefaultObject: {
type: Object,
default() {
return {}
}
},
exampleDefaultArray: {
type: Array,
default() {
return []
}
}
},
或在ES6中:
props: {
exampleDefaultObject: {
type: Object,
default: () => ({})
},
exampleDefaultArray: {
type: Array,
default: () => []
}
},
(对于来这里寻找问题“具有object/array类型的道具必须使用工厂函数返回默认值”中错误解释的人)
请注意,在es6 arrow函数中返回对象时,需要使用括号:
()=>({})
,而不是我的类组件中TS格式的()=>{}
。它解决了我的错误。
感谢以上解决方案(&F)
@Prop({default(){return []}}) private markerList!: Array<markerType>;
@Prop({default(){return[]}})私有标记列表!:阵列;
这些情况需要生成默认值的函数,而不是对象或数组:
obj_param:{
type:Object,
default:()=>{}
},
array_param:{
type:Array,
default:()=>[]
},
这是出于性能原因,因为函数仅在需要时调用(没有值,所以使用默认值),而直接值将在每次使用组件时实例化。你是个天才。我试过了,它被正确地复制了,没有控制台错误。非常感谢!!!!!控制台错误被删除!但我现在发现我忘了写非常重要的东西。我忘了写下即使有控制台错误,性能也没有问题。即使有控制台错误消息,它也被正确地深度复制,并且工作正常。但是我只是想删除控制台错误。谢谢,这是我在搜索这个问题时寻找的答案