当前位置: 首页 > news >正文

uniapp子组件向父组件传值

目录

    • 子组件向父组件传值
      • 子组件1
      • 子组件2
    • 父组件
    • 最后

子组件向父组件传值

子组件1

<template><view class="content"><view v-for="(item,index) in list" :key="index">{{item}}</view></view>
</template><script>export default {data() {return {list: []}},components: {},onLoad() {},created() {var data = require('../../static/res.json')this.list = data.fruitDatathis.$emit("getChild1",this.list);},methods: {}}
</script><style>.content {display: flex;flex-direction: column;align-items: center;justify-content: center;}.logo {height: 200upx;width: 200upx;margin-top: 200upx;margin-left: auto;margin-right: auto;margin-bottom: 50upx;}.text-area {display: flex;justify-content: center;}.title {font-size: 36upx;color: #8f8f94;}
</style>

子组件2

<template><view class="content"><view v-for="(item,index) in list" :key="index">{{item}}</view></view>
</template><script>export default {data() {return {list: []}},components: {},onLoad() {},created() {var data = require('../../static/res.json')this.list = data.filmDatathis.$emit("getChild2",this.list);},methods: {}}
</script><style>.content {display: flex;flex-direction: column;align-items: center;justify-content: center;}.logo {height: 200upx;width: 200upx;margin-top: 200upx;margin-left: auto;margin-right: auto;margin-bottom: 50upx;}.text-area {display: flex;justify-content: center;}.title {font-size: 36upx;color: #8f8f94;}
</style>

父组件

<template><view class="content"><view class="btn-container"><button @click="getData(index)" v-for="(item,index) in list" :key="index">{{item}}</button></view><child1 @getChild1 = "getChild1" v-if="current==0"></child1><child2 @getChild2 = "getChild2" v-if="current==1"></child2></view>
</template><script>import child1 from './child1'import child2 from './child2'export default {data() {return {list:['水果','电影'],current:0}},onLoad() {},components: {child1,child2},methods: {getData(index){this.current = index},getChild1(e){console.log(e)},getChild2(e){console.log(e)}}}
</script><style>.content {display: flex;flex-direction: column;align-items: center;justify-content: center;}.btn-container{display: flex;justify-content: space-between;align-items: center;}
</style>

最后

感觉文章好的话记得点个心心和关注和收藏,有错的地方麻烦指正一下,如果需要转载,请标明出处,多谢!!!

http://www.lryc.cn/news/273016.html

相关文章:

  • 过滤特殊 微信昵称
  • LLM、AGI、多模态AI 篇一:开源大语言模型简记
  • 微信小程序中获取用户当前位置的解决方案
  • Vue3-35-路由-路由守卫的简单认识
  • 制药企业符合CSV验证需要注意什么?
  • 再谈动态SQL
  • 【数据结构】树
  • 【Midjourney】AI绘画新手教程(一)登录和创建服务器,生成第一幅画作
  • 对比 PyTorch 和 TensorFlow:选择适合你的深度学习框架
  • Oracle笔记-查看表已使用空间最大空间
  • 大数据HCIE成神之路之特征工程——特征选择
  • python 正则-常见题目
  • 解析:Eureka的工作原理
  • RecyclerView 与 ListView 区别和使用
  • 力扣232. 用栈实现队列
  • 这个方法可以让你把图片无损放大
  • Springboot整合Elastic-job
  • VsCode的介绍和入门
  • C++:自创小游戏
  • AIGC带给开发者的冲击
  • 利用蚁剑钓鱼上线CS
  • 宣传照(私密)勿转发
  • 【Spring】19 AOP介绍及实例详解
  • ES(Elasticsearch)的基本使用
  • 【JVM面试题】Java中的静态方法为什么不能调用非静态方法
  • 对‘float16_t’的引用有歧义
  • Windows重装升级Win11系统后 恢复Mysql数据
  • MySQL之四大引擎、账号管理以及建库
  • shell编程——查找局域网内存活主机
  • python django 个人记账管理系统