defineProps
一,前言
1.在vue2
中,我们使用选项api
中的props
来接受父组件传递来的数据。
2.在vue3
的setup
中,我们使用defineProps
来定义父组件传递的props
二,defineProps
介绍
1.defineProps
是vue3
的一个宏函数,使用时可不导入
2.defineProps
是一个函数,参数与vue2
的props
选项相同
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相关推荐
- error ‘defineProps‘ is not defined no-undef
error 'defineProps' is not defined no-undef 例如defineProps和defineEmits生成no-undef警告 示例.eslintrc.js: mo ...
- 3.vue3.2的父传子defineProps,子传父emits以及ref
1.父传子 <template><div class="container"><!-- 传递数据 这里传了一个string 和 一个list --&g ...
- vue3.0 ref reactive nextTick watch toRaw defineProps defineEmits
目前使用的一些属性持续更新中 ref:推荐定义基本数据类型(ref值也可以是对象,但是一般情况下是对象直接使用reactive更合理). const data = ref( {id: "1& ...
- [@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 ...
- vue3 ts defineProps
ts 使用vue3的defineProps时 props写法 const props = defineProps({ pic: {type: Array as () => Array<st ...
- Vue3中的 defineEmits/defineProps
defineEmits:子传父方法 defineProps:父传子属性值 一.defineEmits用法: 已知:父组件中绑定了两个兄弟组件 要求:点击其中的兄弟组件1以后,兄弟组件2获取兄弟组件1中 ...
- vue3.2props设置默认值【defineProps】
<script setup lang="ts">// ts写法const props = withDefaults(defineProps<{title?: st ...
- 【Vue】 defineProps 与 props
props \ defineProps 1 props 2 defineProps vue 官网 Props 命名:如果一个 prop 的名字很长,建议使用小驼峰 props: { greetingM ...
- vue3的setup语法糖获取 代替 setup方法下的contenxt的属性useAttrs, useSlots, defineEmits拓展defineProps withDefaults
props接收参数有两种 1:props 2:attrs 2选1使用 父组件 <template><div>wedss</div><BaseScrollLis ...
最新文章
- valgrind-yyds——memcheck检查程序中的内存问题,如泄漏、越界、非法指针等。
- 转:小城也有好去处(3):云南建水 - 丽江大理之后第三城
- func_num_args()
- JGroups 入门实践(转)
- TabLayout让Fragment在ViewPager中的滑动切换更优雅
- C++---两数之和
- oa提醒模块要素_OA系统进军贸易行业,高效完成CRM管理
- python中的sklearn教程_python作业之sklearn
- Javascript链式调用案例
- 多个相机间的对齐之为什么需要对齐,对齐有什么好处?
- 30 岁的超级玛丽怎样改变了游戏行业?
- 下载laravel框架
- 【Axure高保真原型】移动端地图模板
- FESCO数字一体化建设项目简介
- vue项目中通过WebSocket实现实时消息提示及遇到的问题
- python最终目标_Python晋级之路-工欲善其事必先利其器
- oracle中execute是什么意思,oracle中EXECUTEIMMEDIATE是什么意思?如何使用,请用自己......
- Tiled Map Editor(一)
- Springcloud个人总结
- ceph(ceph是什么意思)
热门文章
- Code Complete《代码大全》读书笔记
- mmdetection3d kitti (持续更新)
- 无需UnityHub启动unity 项目
- 修改xcode字体大小(Mac)
- 秘钥认证登录CentOS 7
- Unity 安卓 Unable to initialize the Unity Engine / No Enough Space to install unity
- 国内外最好的12款项目管理系统优劣势分析
- 秘鲁地震与Stallman失踪之谜
- 中公教育计算机老师笔试面试题,2020下高中信息技术教师资格证面试试题及答案【1月9日下午】...
- 横向扩展 纵向扩展 数据库_理解数据库扩展模式的指南