一,前言

1.在vue2中,我们使用选项api中的props来接受父组件传递来的数据。

2.在vue3setup中,我们使用defineProps来定义父组件传递的props

二,defineProps介绍

1.definePropsvue3的一个宏函数,使用时可不导入

2.defineProps是一个函数,参数与vue2props选项相同

defineProps({msg:String,name:{type:String,require:true,default:'xxxx'}
})

3.定义后props可直接在模板中使用,或者在setup其他地方使用

<template><span>{{ msg }}</span></template>
const props= defineProps({msg:String,name:{type:String,default:'xxxx'}
})console.log(props.name)
props.name = 123 //不能修改,声明的props的值是只读readonly的

4.defineProps返回的props对象,是一个proxy对象,所有特性和reactive基本相同,只不过由defineProps定义出的props对象的值是只读的,还有在模板上可以单独属性直接使用

四,在typescript中为props添加标注类型

1.若不使用typescript来写可忽略,

2.当使用 <script setup> 时,defineProps() 宏函数支持从它的参数中推导类型,即我们可以向上面一样,定义defineProps

defineProps({msg:String,name:{type:String,default:'xxxx'}
})

3.更直接的方法是通过泛型参数来定义 props 的类型,注意当没有使用可选属性时,那么在使用该组件时,一定要传该属性,否则会报错

interface Props {msg:string, //相当于require:true , 在使用该组件时一定要传改属性name:stringage?:number //require:false
}defineProps<Props>()

4.由于接口不能设置默认值,因此这种方式失去了设置默认值的能力,vue3提供了withDefaults 编译器宏解决

withDefaults(defineProps<Props>(), {name: 'angle',
});

5.在使用withDefaults 编译器宏设置默认值时,对于引用类型,需要使用函数形式

withDefaults(defineProps<Props>(), {list: ()=>[],
});

五,注意事项

1.defineProps只能在setup中使用,且只能在setup的顶层使用,不能在局部作用域使用

2.vue2一样的原则,defineProps声明的props的值是只读readonly的,不能修改,也就是说,当传过来的是原始值时不能修改,当是对象类型时,不能修改对象类型的引用地址。

3.注意,在使用defineProps泛型参数来定义props时,泛型本身不能是外部引入的类型

defineProps相关推荐

  1. error ‘defineProps‘ is not defined no-undef

    error 'defineProps' is not defined no-undef 例如defineProps和defineEmits生成no-undef警告 示例.eslintrc.js: mo ...

  2. 3.vue3.2的父传子defineProps,子传父emits以及ref

    1.父传子 <template><div class="container"><!-- 传递数据 这里传了一个string 和 一个list --&g ...

  3. vue3.0 ref reactive nextTick watch toRaw defineProps defineEmits

    目前使用的一些属性持续更新中 ref:推荐定义基本数据类型(ref值也可以是对象,但是一般情况下是对象直接使用reactive更合理). const data = ref( {id: "1& ...

  4. [@vue/compiler-sfc] defineProps is a compiler macro and no longer needs to be imported

    Vue Vite项目启动后,控制台报错: [@vue/compiler-sfc] defineProps is a compiler macro and no longer needs to be i ...

  5. vue3 ts defineProps

    ts 使用vue3的defineProps时 props写法 const props = defineProps({ pic: {type: Array as () => Array<st ...

  6. Vue3中的 defineEmits/defineProps

    defineEmits:子传父方法 defineProps:父传子属性值 一.defineEmits用法: 已知:父组件中绑定了两个兄弟组件 要求:点击其中的兄弟组件1以后,兄弟组件2获取兄弟组件1中 ...

  7. vue3.2props设置默认值【defineProps】

    <script setup lang="ts">// ts写法const props = withDefaults(defineProps<{title?: st ...

  8. 【Vue】 defineProps 与 props

    props \ defineProps 1 props 2 defineProps vue 官网 Props 命名:如果一个 prop 的名字很长,建议使用小驼峰 props: { greetingM ...

  9. vue3的setup语法糖获取 代替 setup方法下的contenxt的属性useAttrs, useSlots, defineEmits拓展defineProps withDefaults

    props接收参数有两种 1:props 2:attrs 2选1使用 父组件 <template><div>wedss</div><BaseScrollLis ...

最新文章

  1. valgrind-yyds——memcheck检查程序中的内存问题,如泄漏、越界、非法指针等。
  2. 转:小城也有好去处(3):云南建水 - 丽江大理之后第三城
  3. func_num_args()
  4. JGroups 入门实践(转)
  5. TabLayout让Fragment在ViewPager中的滑动切换更优雅
  6. C++---两数之和
  7. oa提醒模块要素_OA系统进军贸易行业,高效完成CRM管理
  8. python中的sklearn教程_python作业之sklearn
  9. Javascript链式调用案例
  10. 多个相机间的对齐之为什么需要对齐,对齐有什么好处?
  11. 30 岁的超级玛丽怎样改变了游戏行业?
  12. 下载laravel框架
  13. 【Axure高保真原型】移动端地图模板
  14. FESCO数字一体化建设项目简介
  15. vue项目中通过WebSocket实现实时消息提示及遇到的问题
  16. python最终目标_Python晋级之路-工欲善其事必先利其器
  17. oracle中execute是什么意思,oracle中EXECUTEIMMEDIATE是什么意思?如何使用,请用自己......
  18. Tiled Map Editor(一)
  19. Springcloud个人总结
  20. ceph(ceph是什么意思)

热门文章

  1. Code Complete《代码大全》读书笔记
  2. mmdetection3d kitti (持续更新)
  3. 无需UnityHub启动unity 项目
  4. 修改xcode字体大小(Mac)
  5. 秘钥认证登录CentOS 7
  6. Unity 安卓 Unable to initialize the Unity Engine / No Enough Space to install unity
  7. 国内外最好的12款项目管理系统优劣势分析
  8. 秘鲁地震与Stallman失踪之谜
  9. 中公教育计算机老师笔试面试题,2020下高中信息技术教师资格证面试试题及答案【1月9日下午】...
  10. 横向扩展 纵向扩展 数据库_理解数据库扩展模式的指南