Vue子组件内的props对象参数配置方法

作者:雨季mo浅忆 时间:2024-04-30 10:39:09 

这篇文章主要介绍了Vue子组件内的props对象里的default参数是如何定义

Array、Object、或Function默认值的正确写法说明,具有很好的参考价值

Vue子组件内的props对象参数配置方法

一、简单数据类型

1、布尔类型 Boolean

正确写法 :

props: {
   demoBoo: {
     type: Boolean,
     default: true,
   },
 },

2、数字类型 Number

正确写法 :

props: {
   demoNum: {
     type: Number,
     default: 1,
   },
 },

3、字符串类型 String

正确写法 :

props: {
   demoStr: {
     type: String,
     default: 'hello',
   },
 },

二、复杂数据类型

1、数组 Array

错误写法 :

props: {
   demoArr: {
     typeof: Array,
     default: [],
   },
 },

Eslint 语法报错 :

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

Vue子组件内的props对象参数配置方法

正确的常规写法 :

props: {
   demoArr: {
     type: Array,
     default: function () {
       return [];
     },
   },
 },

或是用 箭头函数 :

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

2、对象 Object

错误写法 :

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

正确的常规写法 :

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

或是用 箭头函数 :( 注意 : 这里的对象一定要用小括号包裹起来( { } )

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

3、函数 Function

正确写法 :

props: {
   demoFun: {
     type: Function,
     default: () => {},
   },
 },

补充知识 :Vue 传参 props 里面为什么要带 type , 还有 default ?

Vue子组件内的props对象参数配置方法

这个是子组件, 写 type 的 意思 是 swiperDate 传过来的数据类型是 数组 ,

default就是 表示 不传 ,默认返回 的 [ ] , 空数组 .

Vue子组件内的props对象参数配置方法

这种就是 表示 传的数据类型Number, 不传默认是 数字 0 。

来源:https://blog.csdn.net/weixin_58099903/article/details/126428326

标签:Vue,子组件,props
0
投稿

猜你喜欢

  • SQL中distinct的用法(四种示例分析)

    2024-01-15 17:18:40
  • 详解ASP中断开记录集的使用方法

    2008-02-13 08:35:00
  • Django 删除upload_to文件的步骤

    2022-03-23 05:47:14
  • Centos MySQL 5.7安装、升级教程

    2024-01-19 10:24:03
  • 解决vue-cli + webpack 新建项目出错的问题

    2024-04-29 13:10:56
  • mysql回表致索引失效案例讲解

    2024-01-21 01:28:48
  • windows下cx_Freeze生成Python可执行程序的详细步骤

    2022-12-06 05:27:57
  • asp如何分页显示数据库查询结果?

    2009-11-22 19:23:00
  • Python导入模块包原理及相关注意事项

    2023-01-26 04:46:31
  • MySql获取某个字段存在于哪个表的sql语句

    2024-01-18 23:12:11
  • Python修改Excel数据的实例代码

    2021-05-24 12:40:29
  • python使用Tesseract库识别验证

    2023-05-06 12:31:51
  • PassWord输入框代码分享

    2024-04-18 09:34:36
  • 在python下实现word2vec词向量训练与加载实例

    2022-06-12 23:49:05
  • PHP获取当前相对于域名目录的方法

    2023-08-19 18:47:31
  • Windows10下mysql 8.0.19 winx64安装教程及修改初始密码

    2024-01-26 11:03:37
  • Python集合之set和frozenset的使用详解

    2021-06-26 22:41:20
  • MySQL中slave_exec_mode参数详解

    2024-01-18 07:36:34
  • python入门语句基础之if语句、while语句

    2023-08-14 01:22:55
  • 多个datatable共存造成多个表格的checkbox都被选中

    2024-05-11 09:18:42
  • asp之家 网络编程 m.aspxhome.com