vue子父之间传值方式
子传父
vue子传父使用$emit传值
子组件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | < template > < div > < button @click="toParent">点击传到父级</ button > </ div > </ template > < script > export default { name: 'child', methods: { toParent () { this.$emit('fromChild', 'child') } } } </ script > |
父组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | < template > < div > < p >子级传过来的值:{{childVal}}</ p > < child @fromChild="getChild"></ child > </ div > </ template > < script > import child from "@/components/child"; export default { name: 'parent', data () { return { childVal: '' } }, components: { child }, methods: { getChild (v) { this.childVal = v; } } } </ script > |
传过来啦
父传子
子组件使用props接收 接收时还可以设置默认值 当没获取到值时 会使用设置的默认值
父组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | < template > < div > < child :tochild="parentVal"></ child > </ div > </ template > < script > import child from "@/components/child"; export default { name: 'parent', data () { return { parentVal: 'parent', } }, components: { child } } </ script > |
子组件
1 2 3 4 5 6 7 8 9 10 11 12 13 | < template > < div > < p >父级传过来的值:{{tochild}}</ p > </ div > </ template > < script > export default { name: 'child', props: { tochild: String } } </ script > |
vue 自定义组件(父级,子级组件)传参
父组件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | < template > < div > < p >我在父组件</ p > < p @click="alertClick('值')">点击向子组件传值</ p > < alert v-on:Event_s="backRequest" ref="alert"></ alert > //ref 声明组件 v-on回调事件名 </ div > </< template > < script > import material from 'alert';//引入子组件,子组件路径 export default { data() { return {} }, //注册组件 components: { alert: alert, }, methods: { //使用this.$refs 调用alert组件 的myAlert方法 alertClick(val){ this.$refs.alert.myAlert(val); }, backRequest: function (val) { //接收子组件返回的值,val值应该为:我是子组件返回给父组件的内容 alert(val) }, }, } </ script > |
子组件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | < template > < p >我在子组件</ p > </ template > < script > export default { data() { return {} }, methods: { myAlert(val){ alert(val); this.return('我是子组件返回给父组件的内容')//调用下面return方法 }, //使用 this.$emit 返回给父级组件内容 return(data) { this.$emit('Event_s',data);//Event_s 为父级组件用v-on声明的名称 }, }, } </ script > |
Vue高级组件传值之 provide/inject
父组件 <script> export default { name: "parent", provide() { return { parent:'Hellow World', hello:this.hello } }, methods:{ hello(){ console.log("Hello World") }, } }; </script>
子组件 <template> <div>{{parent}}</div> </template> <script> export default { name: "great-grandson", inject:["parent","hello"], mounted() { this.hello(); } }; </script>
子组件调用父组件方法
父组件
<template> <div> <child :fatherMethod="fatherMethod"></child> </div> </template> <script> import child from '@/components/child'; export default { components: { child }, methods: { fatherMethod() { console.log('fatherMethod'); } } }; </script>
子组件
<template> <div> <button @click="childMethod">点击</button> </div> </template> <script> export default { props: { fatherMethod: { type: Function, default: null } }, methods: { childMethod() { if (this.fatherMethod) { this.fatherMethod(); } } } }; </script>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通