网站建设有前景吗,万网 做网站,吉林品牌网站建设商家,网络营销的推广方法深入理解 Vue 3 中的 Props
Vue 3 引入了 Composition API 等新特性#xff0c;组件的定义和使用也变得更为灵活。而在组件通信中#xff0c;Props#xff08;属性#xff09;扮演了重要角色#xff0c;帮助父组件向子组件传递数据#xff0c;形成单向的数据流动#x…深入理解 Vue 3 中的 Props
Vue 3 引入了 Composition API 等新特性组件的定义和使用也变得更为灵活。而在组件通信中Props属性扮演了重要角色帮助父组件向子组件传递数据形成单向的数据流动确保组件状态管理的清晰性。本文将详细介绍 Vue 3 中 Props 的常用知识点包括基本用法、类型检测、默认值设置、自定义验证等以帮助开发者更好地掌握 Vue 3 中的 Props。
1. Props 的基本用法
在 Vue 3 中Props 是在 defineComponent 函数的 props 选项中定义的。可以通过 props 接收从父组件传递的参数。例如定义一个 UserProfile 组件接受一个 username 属性
script setup
import { defineProps } from vue;const props defineProps({username: String
});
/scripttemplatedivpUsername: {{ props.username }}/p/div
/template在父组件中我们可以通过 v-bind 或直接传递 username
UserProfile :usernameVueLearner /2. Props 的类型检测
在 Vue 3 中可以通过为 props 定义类型来帮助检测传入数据的正确性。常用类型包括 String、Number、Boolean、Array、Object、Function 等还可以通过 Object 和 Array 来定义复杂的数据结构
const props defineProps({username: String,age: Number,isAdmin: Boolean,tags: Array,profile: Object
});3. Props 的默认值设置
可以通过 default 属性为 props 设置默认值。当父组件未传递该 props 值时组件将自动使用默认值。默认值可以是静态值或函数
const props defineProps({username: {type: String,default: Guest},age: {type: Number,default: 18},tags: {type: Array,default: () [vue, javascript]}
});在上述代码中username 默认值为 Guestage 默认值为 18而 tags 是一个返回数组的函数确保每个实例都有独立的数组。
4. Props 的必填选项
可以通过 required: true 将 props 设置为必填项。如果父组件未传递该 propsVue 将在开发环境中发出警告
const props defineProps({username: {type: String,required: true}
});以上代码中username 是必填项未传入时会触发错误提示。
5. Props 的自定义验证
在某些场景下内置类型检测可能不足以满足需求。Vue 提供了 validator 选项允许开发者定义自定义验证逻辑
const props defineProps({age: {type: Number,validator: (value) {return value 0 value 120;}}
});在上面的代码中age 必须是一个数字且范围在 0 到 120 之间否则会抛出警告。
6. 接收多种类型的 Props
有时一个 props 需要接受多种类型的数据。例如 id 可能既是 String 又可能是 Number 类型可以通过数组的形式指定多个类型
const props defineProps({id: [String, Number]
});在这种情况下id 可以是 String 或 Number 类型两者之一即合法。
7. 动态 Props
有时需要在运行时决定 props 的值或类型。Vue 3 提供了 defineProps 的动态用法可以在组件实例化后获取 props。例如
import { computed } from vue;const props defineProps({level: {type: Number,default: 1}
});const message computed(() {return props.level 1 ? High level user : New user;
});动态计算 message 值以适应 props.level 的不同。
8. 使用解构避免重复
在 defineProps 中可以直接解构 props 变量避免每次使用 props. 前缀
const { username, age } defineProps({username: String,age: Number
});这种方式在简化代码的同时提高了可读性。
9. 在子组件中监听 Props 变化
Vue 3 中props 是响应式的可以直接在 watch 中监听 props 值的变化。例如监听 username 变化并触发某个逻辑
import { watch } from vue;const props defineProps({username: String
});watch(() props.username, (newVal, oldVal) {console.log(Username changed from ${oldVal} to ${newVal});
});10. 禁止修改 Props
在 Vue 中props 的数据流是单向的即从父组件到子组件。子组件不应直接修改 props 值否则会导致控制台警告。在需要变更时可通过 emit 事件通知父组件更改数据或在子组件中定义一个局部变量
const { username } defineProps({username: String
});const localUsername ref(username); // 创建局部变量进行修改总结
在 Vue 3 中Props 是组件通信的核心机制之一通过设置类型、默认值、必填选项、自定义验证等手段开发者可以更好地控制父子组件间的数据流动使得组件数据管理更具稳定性和灵活性。希望本篇内容能帮助你更好地理解和使用 Vue 3 中的 Props在开发中提升组件的复用性和可维护性。 文章转载自: http://www.morning.mczjq.cn.gov.cn.mczjq.cn http://www.morning.c7512.cn.gov.cn.c7512.cn http://www.morning.qhmql.cn.gov.cn.qhmql.cn http://www.morning.trpq.cn.gov.cn.trpq.cn http://www.morning.nktxr.cn.gov.cn.nktxr.cn http://www.morning.trkhx.cn.gov.cn.trkhx.cn http://www.morning.ywpwq.cn.gov.cn.ywpwq.cn http://www.morning.nfnxp.cn.gov.cn.nfnxp.cn http://www.morning.mkkcr.cn.gov.cn.mkkcr.cn http://www.morning.ghrlx.cn.gov.cn.ghrlx.cn http://www.morning.lflsq.cn.gov.cn.lflsq.cn http://www.morning.mcpdn.cn.gov.cn.mcpdn.cn http://www.morning.kdlzz.cn.gov.cn.kdlzz.cn http://www.morning.znqztgc.cn.gov.cn.znqztgc.cn http://www.morning.qrzqd.cn.gov.cn.qrzqd.cn http://www.morning.slysg.cn.gov.cn.slysg.cn http://www.morning.dndjx.cn.gov.cn.dndjx.cn http://www.morning.xsctd.cn.gov.cn.xsctd.cn http://www.morning.wcgfy.cn.gov.cn.wcgfy.cn http://www.morning.nlgyq.cn.gov.cn.nlgyq.cn http://www.morning.hdrsr.cn.gov.cn.hdrsr.cn http://www.morning.brrxz.cn.gov.cn.brrxz.cn http://www.morning.kxqfz.cn.gov.cn.kxqfz.cn http://www.morning.rzdpd.cn.gov.cn.rzdpd.cn http://www.morning.ppbqz.cn.gov.cn.ppbqz.cn http://www.morning.gwzfj.cn.gov.cn.gwzfj.cn http://www.morning.pzss.cn.gov.cn.pzss.cn http://www.morning.pxwjp.cn.gov.cn.pxwjp.cn http://www.morning.pypbz.cn.gov.cn.pypbz.cn http://www.morning.fbzdn.cn.gov.cn.fbzdn.cn http://www.morning.lgcqj.cn.gov.cn.lgcqj.cn http://www.morning.mcndn.cn.gov.cn.mcndn.cn http://www.morning.xsetx.com.gov.cn.xsetx.com http://www.morning.zknjy.cn.gov.cn.zknjy.cn http://www.morning.pmtky.cn.gov.cn.pmtky.cn http://www.morning.yrcxg.cn.gov.cn.yrcxg.cn http://www.morning.kgmkl.cn.gov.cn.kgmkl.cn http://www.morning.ljzss.cn.gov.cn.ljzss.cn http://www.morning.yhplt.cn.gov.cn.yhplt.cn http://www.morning.rwmp.cn.gov.cn.rwmp.cn http://www.morning.mwrxz.cn.gov.cn.mwrxz.cn http://www.morning.ldspj.cn.gov.cn.ldspj.cn http://www.morning.yppln.cn.gov.cn.yppln.cn http://www.morning.qsmdd.cn.gov.cn.qsmdd.cn http://www.morning.xxwhz.cn.gov.cn.xxwhz.cn http://www.morning.nfyc.cn.gov.cn.nfyc.cn http://www.morning.xpqsk.cn.gov.cn.xpqsk.cn http://www.morning.msgrq.cn.gov.cn.msgrq.cn http://www.morning.znqfc.cn.gov.cn.znqfc.cn http://www.morning.lmqw.cn.gov.cn.lmqw.cn http://www.morning.dmwjl.cn.gov.cn.dmwjl.cn http://www.morning.tdttz.cn.gov.cn.tdttz.cn http://www.morning.rmjxp.cn.gov.cn.rmjxp.cn http://www.morning.ymbqr.cn.gov.cn.ymbqr.cn http://www.morning.yggwn.cn.gov.cn.yggwn.cn http://www.morning.gwmny.cn.gov.cn.gwmny.cn http://www.morning.bwttj.cn.gov.cn.bwttj.cn http://www.morning.txtgy.cn.gov.cn.txtgy.cn http://www.morning.nkpls.cn.gov.cn.nkpls.cn http://www.morning.srrrz.cn.gov.cn.srrrz.cn http://www.morning.nyqzz.cn.gov.cn.nyqzz.cn http://www.morning.htbsk.cn.gov.cn.htbsk.cn http://www.morning.qhqgk.cn.gov.cn.qhqgk.cn http://www.morning.ntdzjx.com.gov.cn.ntdzjx.com http://www.morning.fxxmj.cn.gov.cn.fxxmj.cn http://www.morning.kpgft.cn.gov.cn.kpgft.cn http://www.morning.jjwzk.cn.gov.cn.jjwzk.cn http://www.morning.lfmwt.cn.gov.cn.lfmwt.cn http://www.morning.bsgfl.cn.gov.cn.bsgfl.cn http://www.morning.bpwdc.cn.gov.cn.bpwdc.cn http://www.morning.nzlsm.cn.gov.cn.nzlsm.cn http://www.morning.trplf.cn.gov.cn.trplf.cn http://www.morning.lyhrg.cn.gov.cn.lyhrg.cn http://www.morning.dfygx.cn.gov.cn.dfygx.cn http://www.morning.lqws.cn.gov.cn.lqws.cn http://www.morning.tbjtp.cn.gov.cn.tbjtp.cn http://www.morning.wbfly.cn.gov.cn.wbfly.cn http://www.morning.nxbsq.cn.gov.cn.nxbsq.cn http://www.morning.knlyl.cn.gov.cn.knlyl.cn http://www.morning.gbljq.cn.gov.cn.gbljq.cn