vue props default Array以及object

818 阅读1分钟

1、错误写法

demo:{
  type:Array,
  default:[]
}

eslint语法报错: Invalid default value for prop “demo”: Props with type Object/Array must use a factory function to return the default value.

2、正确的写法应该是:

demo: {
    type: Array,
    default: function () {
        return []
    }
}

或是用箭头函数:

demo: {
  type: Array,
  default: () => []
}

3、对象的箭头函数写法:

demoObj: {
  type: Object,
  default: () => ({})
}

或是常规


demoObj: {
    type: Object,
    default: function () {
    return {}
    }
}

错误的写法

demoObj: () => {}