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

做网站建设优化的公司有趣的h5创意设计

做网站建设优化的公司,有趣的h5创意设计,中国八大设计院排名,宝塔 wordpress在 Vue 中,父子组件的通信是组件间数据传递的核心机制之一。以下是常见的几种通信方式及适用场景: 1. Props(父 → 子) 用途:父组件通过 props 向子组件传递数据,单向数据流。语法: 父组件&…

在 Vue 中,父子组件的通信是组件间数据传递的核心机制之一。以下是常见的几种通信方式及适用场景:


1. Props(父 → 子)

  • 用途:父组件通过 props 向子组件传递数据,单向数据流。
  • 语法
    • 父组件:通过 v-bind 或简写 : 绑定属性。
    <ChildComponent :message="parentMessage" />
    
    • 子组件:声明 props 接收数据。
    export default {props: ['message']
    }
    
  • 示例
    <!-- Parent.vue -->
    <template><Child :text="helloFromParent" @click="handleClick" />
    </template><script>
    import Child from './Child.vue';
    export default {components: { Child },data() {return { helloFromParent: 'Hello!' };}
    };
    </script><!-- Child.vue -->
    <template><div @click="sendMessageToParent">{{ text }}</div>
    </template><script>
    export default {props: ['text'],methods: {sendMessageToParent() {this.$emit('custom-event', 'Message from Child');}}
    };
    </script>
    

2. Events(子 → 父)

  • 用途:子组件通过自定义事件向父组件发送消息,触发父组件的方法。

  • 语法

    • 子组件:使用 $emit 触发事件。
    this.$emit('update', newValue);
    
    • 父组件:监听事件并绑定处理方法。
    <Child @update="handleUpdate" />
    
  • 示例

    // Child.vue 中触发事件
    methods: {sendToParent() {this.$emit('child-event', 'Data from Child');}
    }
    
    // Parent.vue 监听事件
    <Child @child-event="parentMethod" />
    

3. Slot(插槽)

  • 用途:父组件通过插槽向子组件传递内容(HTML 或组件),子组件定义插槽位置。

  • 分类

    • 默认插槽<slot></slot>
    • 具名插槽<slot name="header"></slot>
    • 作用域插槽:允许父组件传递数据到插槽内容中。
  • 示例(作用域插槽)

    <!-- Child.vue -->
    <template><div><h3>Child Component:</h3><slot name="content" :data="childData"></slot></div>
    </template><script>
    export default {data() {return { childData: 'This is from Child' };}
    };
    </script>
    
    <!-- Parent.vue 使用插槽 -->
    <Child><template #content="{ data }"><p>{{ data }}</p></template>
    </Child>
    

4. 自定义事件总线(非父子组件间通信)

  • 用途:适用于任意组件间的通信(需注意 Vue 3 中已废弃 EventBus,建议用 mittVue.observable 替代)。
  • 示例
    // 创建事件总线
    const EventBus = Vue.observable({});// 组件 A 发送事件
    EventBus.$emit('event-name', payload);// 组件 B 监听事件
    EventBus.$on('event-name', (payload) => {console.log(payload);
    });
    

5. Provide / Inject(祖先 → 后代)

  • 用途:跨层级组件通信,适用于祖父组件向孙组件传递数据。
  • 语法
    • 父组件:通过 provide 提供数据。
    provide() {return { sharedData: this.sharedData };
    }
    
    • 子组件:通过 inject 注入数据。
    inject(['sharedData']);
    
  • 示例
    <!-- GrandParent.vue -->
    <template><Parent />
    </template><script>
    export default {provide() {return { message: 'Hello from GrandParent' };}
    };
    </script><!-- Child.vue -->
    <template>{{ injectedMessage }}
    </template><script>
    export default {inject: ['message'],computed: {injectedMessage() {return this.message;}}
    };
    </script>
    

6. Ref 获取组件实例

  • 用途:通过 ref 获取子组件实例,直接调用方法或访问数据(不推荐频繁使用)。
  • 示例
    <!-- Parent.vue -->
    <template><Child ref="childRef" /><button @click="callChildMethod">Call Child Method</button>
    </template><script>
    import Child from './Child.vue';
    export default {components: { Child },methods: {callChildMethod() {this.$refs.childRef.childMethod();}}
    };
    </script>
    

总结

  • 父子通信首选props(父→子) + events(子→父)。
  • 内容分发:使用插槽(尤其是作用域插槽)。
  • 复杂状态管理:考虑使用 VuexPinia(集中式状态管理)。
  • 跨层级通信provide/inject 或事件总线(根据项目规模选择)。

根据具体需求选择最适合的通信方式,保持代码的可维护性和可读性。


文章转载自:

http://5YoiWXZY.Lmcrc.cn
http://JKAyLvM2.Lmcrc.cn
http://eCfWPxcM.Lmcrc.cn
http://LCokYBkV.Lmcrc.cn
http://lqYrYqAH.Lmcrc.cn
http://DtSYg0Z5.Lmcrc.cn
http://1kDytnYQ.Lmcrc.cn
http://aEyi40Fv.Lmcrc.cn
http://f8rfB80a.Lmcrc.cn
http://YEuR5eFA.Lmcrc.cn
http://PLtUDjLb.Lmcrc.cn
http://zEgTFd2A.Lmcrc.cn
http://JFPpaS1y.Lmcrc.cn
http://Yabi6inw.Lmcrc.cn
http://kWs5TPqh.Lmcrc.cn
http://rEVBHO4l.Lmcrc.cn
http://SY64fDmI.Lmcrc.cn
http://8wpeG9iW.Lmcrc.cn
http://c9eBZ0Jn.Lmcrc.cn
http://GYsDzuUS.Lmcrc.cn
http://NpvIVUiI.Lmcrc.cn
http://n1ueQn1s.Lmcrc.cn
http://6h8M5VPv.Lmcrc.cn
http://GqvsBuej.Lmcrc.cn
http://8W7JWRzS.Lmcrc.cn
http://jrsrAUpL.Lmcrc.cn
http://zjzWccHy.Lmcrc.cn
http://CPjbAD1n.Lmcrc.cn
http://Wi0nOnvG.Lmcrc.cn
http://BpzlfKFv.Lmcrc.cn
http://www.dtcms.com/wzjs/639855.html

相关文章:

  • 网站整体结构南宁 建网站 公司
  • 网站备案手续英语做美食网站
  • 网站设计与网页制作模板做一个企业网站价格
  • 二七网站建设潍坊专利申请
  • 网站建设方案计划书人员规划怎么做盗号网站
  • 网站建设怎么添加背景音乐凡科互动官网登录入口网页版
  • 100种增加网站流量的方法wordpress 开发指南
  • 深圳网站设计制作元现在进出重庆最新规定
  • 大连做网站哪家好一点珠海互联网推广
  • wordpress网站描述网站开发 项目内容
  • dede做的网站打不开织梦调用wordpress
  • 网站域名怎么快速备案价格用阿里云做网站注意事项
  • 企业网站推广公司 知乎做网站借用网络图片不违法吧
  • 织梦摄影网站模板wordpress图片属性添加
  • 永久免费无代码开发平台福建网站优化
  • 什么网站都有漏洞dede网站乱码
  • 模板板网站管理信息系统有哪些
  • 展示型网站建设价格在线设计平台教学
  • 电子商务网站登录小红书怎么做关键词排名优化
  • 广州网站制作培训wordpress 支付宝插件下载
  • 有趣的网站官网云服务器管理
  • 如何自己建设商城网站代做网站和说明书
  • 济南建设官方网站做平台网站多少钱
  • ai网站大全成都定制企业网站制作
  • 外贸企业论坛网站建设便宜域名购买
  • 企业网站建设服务无锡网站seo外包
  • 湖南网站快速开发中小企业管理软件
  • 如何建设网站平台建筑公司做网站买空间多大合适
  • 做网站拍摄照片用什么佳能相机好织梦txt网站地图制作
  • 网站公司 转型中国企业100强排名