vue props default Array或是Object的正确写法

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: () => {}
原创文章 103 获赞 128 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_42991509/article/details/98058760