vue组件传值 父传子、子传父(3种)、非父子传值

2023-10-24 17:58

本文主要是介绍vue组件传值 父传子、子传父(3种)、非父子传值,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

父组件向子组件传值

直接在子组件中通过props来接收

父组件:

<template><div class="home"><img alt="Vue logo" src="../assets/logo.png" /><HelloWorld :obj1="obj" :mes1="mes"></HelloWorld></div>
</template><script>
import HelloWorld from "@/components/HelloWorld.vue";export default {name: "home",data() {return {obj: {name: "hello",age: 12,last: "父向子传的值"},mes: "我是父组件的内容",};},components: {HelloWorld}
};
</script>

 
子组件

<template><div class="hello"><h1>{{ mes1 }}</h1><h2>{{obj1.last}}</h2></div>
</template><script>
export default {name: "HelloWorld",props: {obj1: Object,mes1: String},
};
</script>

 
 
 

子组件向父组件传值(3种)

通过$.emit

父组件:

<template><div class="home"><img alt="Vue logo" src="../assets/logo.png" /><HelloWorld @setValue="getValue"></HelloWorld><h1>{{value}}</h1></div>
</template><script>
import HelloWorld from "@/components/HelloWorld.vue";export default {name: "home",data() {return {value: [],};},components: {HelloWorld},methods: {getValue(value) {this.value = value;}},
};
</script>

子组件:

<template><div class="hello"><h1 @click="change" ref="h1">just click me</h1></div>
</template><script>
export default {name: "HelloWorld",data() {return {val: "子组建向父组件传的值"};},// mounted () {//   this.change();// },methods: {change(){this.$emit("setValue",this.val);this.$refs.h1.setAttribute('class','backcolor');}}
};
</script><style scoped lang="less">
.backcolor {color: red;
}
</style>

gif效果图:
在这里插入图片描述

 

通过ref属性

调用子组件方法时可以传值,ex:
this.$refs.xxx.someMeth(param)

父组件:

<template><div><h1>父组件</h1><Child ref="child"></Child><h4>{{mes}}</h4></div>
</template><script>
import child from '../components/child'
export default {data() {return {mes: ''};},components:{Child: child},mounted() {this.mes = this.$refs.child.message;},
};
</script>
<style lang='less' scoped>
</style>

子组件:

<template><div></div>
</template><script>
export default {data() {return {message: '我是子组件的内容'};},
};
</script>
<style lang='scss' scoped>
</style>

 

通过slot作用域插槽

ps:只要出现多个插槽,始终为所有的插槽使用完整的基于 template 的语法,即v-slot 只能添加在 <template> 上 并绑定相应的name,v-slot:name,单个插槽可以直接在组件上使用。插槽详解

父组件(下面props为自己定义的变量):

<template><div><hello v-slot:default="props"><template><div v-for="(item,index) in props.people" :key="index" >{{item}}-{{item.name}}</div></template></hello></div>
</template><script>
import hello from "@/components/test.vue";
export default {components: {hello: hello},
};
</script>
<style scoped>
</style>

子组件:

<template><div><span>i'm child</span><template><!-- 此处插槽的内容会被覆盖 --><slot :people="people">{{people[0].name}}</slot> </template></div>
</template><script>
export default {data () {return {people:[{name: '张三',age: 20,sex: '男'},{name: '李四',age: 20,sex: '女'},{name: '王二',age: 20,sex: '男'}]}},
};
</script>
<style lang='scss' scoped>
</style>

效果:
在这里插入图片描述
 
 
 

非父子组件传值(也可以通过vuex共享数据)

公共实例文件bus.js,作为公共数控中央总线:

import Vue from 'vue'export default new Vue()

第一个组件 first.vue:

<template><div><h1>firstPage</h1> </div>
</template><script>
import bus from '../bus'
export default {data() {return {message: '我是firstPage的内容'};},beforeDestroy() {bus.$emit('textFromPage1',this.message);},
};
</script>
<style scoped>
</style>

第二个组件second.vue:

<template><div><h1>secondPage</h1> <h4>{{mes}}</h4></div>
</template><script>
import bus from "../bus";
export default {data() {return {mes: ""};},created() {bus.$on("textFromPage1", (val) => {this.mes = val;});}
};
</script>
<style scoped>
</style>

 
 
博文参考:
https://www.cnblogs.com/jin-zhe/p/9291071.html

这篇关于vue组件传值 父传子、子传父(3种)、非父子传值的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/276840

相关文章

Vue和React受控组件的区别小结

《Vue和React受控组件的区别小结》本文主要介绍了Vue和React受控组件的区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录背景React 的实现vue3 的实现写法一:直接修改事件参数写法二:通过ref引用 DOMVu

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

Vue3绑定props默认值问题

《Vue3绑定props默认值问题》使用Vue3的defineProps配合TypeScript的interface定义props类型,并通过withDefaults设置默认值,使组件能安全访问传入的... 目录前言步骤步骤1:使用 defineProps 定义 Props步骤2:设置默认值总结前言使用T

基于Python Playwright进行前端性能测试的脚本实现

《基于PythonPlaywright进行前端性能测试的脚本实现》在当今Web应用开发中,性能优化是提升用户体验的关键因素之一,本文将介绍如何使用Playwright构建一个自动化性能测试工具,希望... 目录引言工具概述整体架构核心实现解析1. 浏览器初始化2. 性能数据收集3. 资源分析4. 关键性能指

Olingo分析和实践之OData框架核心组件初始化(关键步骤)

《Olingo分析和实践之OData框架核心组件初始化(关键步骤)》ODataSpringBootService通过初始化OData实例和服务元数据,构建框架核心能力与数据模型结构,实现序列化、URI... 目录概述第一步:OData实例创建1.1 OData.newInstance() 详细分析1.1.1

从入门到精通详解LangChain加载HTML内容的全攻略

《从入门到精通详解LangChain加载HTML内容的全攻略》这篇文章主要为大家详细介绍了如何用LangChain优雅地处理HTML内容,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录引言:当大语言模型遇见html一、HTML加载器为什么需要专门的HTML加载器核心加载器对比表二

前端如何通过nginx访问本地端口

《前端如何通过nginx访问本地端口》:本文主要介绍前端如何通过nginx访问本地端口的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、nginx安装1、下载(1)下载地址(2)系统选择(3)版本选择2、安装部署(1)解压(2)配置文件修改(3)启动(4)

HTML中meta标签的常见使用案例(示例详解)

《HTML中meta标签的常见使用案例(示例详解)》HTMLmeta标签用于提供文档元数据,涵盖字符编码、SEO优化、社交媒体集成、移动设备适配、浏览器控制及安全隐私设置,优化页面显示与搜索引擎索引... 目录html中meta标签的常见使用案例一、基础功能二、搜索引擎优化(seo)三、社交媒体集成四、移动

HTML input 标签示例详解

《HTMLinput标签示例详解》input标签主要用于接收用户的输入,随type属性值的不同,变换其具体功能,本文通过实例图文并茂的形式给大家介绍HTMLinput标签,感兴趣的朋友一... 目录通用属性输入框单行文本输入框 text密码输入框 password数字输入框 number电子邮件输入编程框