简体   繁体   English

错误信息。 “对象/数组类型的道具必须使用工厂函数来返回默认值。”

[英]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这是出于性能原因,因为仅在需要时才调用该函数(没有值,因此使用默认值),而每次使用组件时都会实例化直接值

In TS format in my class component I have used it similarly as follows.在我的类组件中的 TS 格式中,我类似地使用了它,如下所示。 It solved my error.它解决了我的错误。 Thanks to Trick & Katinka for solution above!感谢Trick & Katinka提供上述解决方案!

@Prop({default(){return []}}) private markerList!: Array<markerType>;

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

相关问题 声明类型既不是“ void”也不是“ any”的函数必须返回一个值。 TypeScript / React - A function whose declared type is neither 'void' nor 'any' must return a value. TypeScript/React 错误:message。$ delete不是函数 - Error: message.$delete is not a function return停止一个函数并返回一个值。 但为什么要在变量上使用呢? - return stops a function and returns a value. But why use it over a variable? Joi 错误:ValidationError:“值”必须是对象类型 - Joi error: ValidationError: "value" must be of type object 对象必须返回函数 - Object must return function 在具有最多道具的数组中返回 object - return object in an array with the most props 如何在angularjs工厂中使用JSON文件返回对象数组? - How to return array of object use json file in angularjs factory? angular 中的 TypeScript 错误:声明类型既不是“void”也不是“any”的 function 必须返回一个值 - TypeScript error in angular: A function whose declared type is neither 'void' nor 'any' must return a value 必须从$ get工厂方法返回一个值 - must return a value from $get factory method 发生错误设置`multiple`时,dropdown`value`必须是一个数组。 收到的类型:`[object String]` - getting error Dropdown `value` must be an array when `multiple` is set. Received type: `[object String]`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM