Vue props中的default值正确写法

先看一个警告

[Vue warn]: Invalid default value for prop "content": Props with type Object/Array must use a factory function to return the default value.
// 错误写法1: 会输出undefined且抛出上面的警告
default: [] 或 default: {}

// 错误写法2:会输出undefined
default: () => [] 或 default: () => {}

// 正确写法:
default: () => ([]) 或 default: () => ({})
//或者
default() {
  return {} // []
}

这是因为引擎识别 {} 是函数作用域而非对象,加了小括号引擎就可以识别了

posted @ 2023-02-14 11:50  珞珞9527  阅读(723)  评论(0)    收藏  举报