vue setup
provide和inject 多成层级传输数据(非响应式):
1 2 3 4 5 6 7 8 9 10 | <script setup> //father import { ref, provide } from 'vue' //provide 提供 const name = ref( 5 ) provide( 'name' , name) //provide 默认发送的不是响应数据 </script> <script setup> //son import {inject} from 'vue' //inject 注入 console.log(inject( 'name' ).value) </script> |
defineExpose:暴露方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <template> <son ref= 'outRef' > <son/> //获取ref中的子组件方法Click() <button @click = 'myClick ' ><button/> </template> <script setup> //father import {ref} from 'vue' const outRef = ref( '' ) function myClick(){ outRef.value.Click() } </script> <script setup> //son import {defineExpose } from 'vue' function Click(){ console.log( '@click方法' ) } //将方法暴露出 defineExpose({Click}) </script> |
defineEmits:子调用father方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <template> <son @selectItem = "selectItem" ></son> </template> <script setup> //father function selectItem(num){ console.log(num); } </script> <script setup> //son import {defineEmits} from 'vue' const emit = defineEmits([ 'selectItem' ]) function choice(num){ emit( 'selectItem' , num) } </script> |
defineProps: 传参 Props:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | < template > < son :name="mymsg"> </ son > </ template > < script setup> //father import {ref} from 'vue'< br > const mymsg=ref("holle world") </ script > < template > < div >{{msg.name}}</ div > </ template > < script setup> //son import {defineProps} from 'vue' const msg=defineProps({ // 非ts专有声明 name:{ type:String, default: '' } }) </ script > |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具