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

企业移动网站品牌上海百度公司地址在哪里

企业移动网站品牌,上海百度公司地址在哪里,重庆做网站团队,网络系统管理技能大赛考什么目录 Vue 组件通信背景介绍组件通信方式1. Props 和 Emit2. Provide/Inject3. Vuex/Pinia 状态管理4. EventBus(不推荐) 组件通信最佳实践1. 父子组件通信2. 跨组件通信 常见问题1. Props 单向数据流2. 组件解耦 面试题 Vue 组件通信 背景介绍 组件通信…

目录

  • Vue 组件通信
    • 背景介绍
    • 组件通信方式
      • 1. Props 和 Emit
      • 2. Provide/Inject
      • 3. Vuex/Pinia 状态管理
      • 4. EventBus(不推荐)
    • 组件通信最佳实践
      • 1. 父子组件通信
      • 2. 跨组件通信
    • 常见问题
      • 1. Props 单向数据流
      • 2. 组件解耦
    • 面试题

Vue 组件通信

背景介绍

组件通信是 Vue 应用开发中的重要概念,不同的组件之间需要共享数据、传递事件。Vue 提供了多种组件通信的方式,每种方式都有其适用场景。

组件通信方式

1. Props 和 Emit

// 父组件
<template><child-component:message="parentMessage"@update="handleUpdate"/>
</template><script>
export default {data() {return {parentMessage: 'Hello'}},methods: {handleUpdate(newValue) {this.parentMessage = newValue}}
}
</script>// 子组件
<template><div><p>{{ message }}</p><button @click="sendToParent">发送到父组件</button></div>
</template><script>
export default {props: {message: String},methods: {sendToParent() {this.$emit('update', 'New Message')}}
}
</script>

2. Provide/Inject

// 父组件
<script>
export default {provide() {return {theme: 'dark',toggleTheme: this.toggleTheme}},methods: {toggleTheme() {this.theme = this.theme === 'dark' ? 'light' : 'dark'}}
}
</script>// 子组件
<script>
export default {inject: ['theme', 'toggleTheme']
}
</script>

3. Vuex/Pinia 状态管理

// store/index.js
import { createStore } from 'vuex'export default createStore({state: {count: 0},mutations: {increment(state) {state.count++}},actions: {increment({ commit }) {commit('increment')}}
})// 组件中使用
<script>
export default {computed: {count() {return this.$store.state.count}},methods: {increment() {this.$store.dispatch('increment')}}
}
</script>

4. EventBus(不推荐)

// eventBus.js
import mitt from 'mitt'
export default mitt()// 组件A
import eventBus from './eventBus'export default {methods: {sendMessage() {eventBus.emit('message', 'Hello')}}
}// 组件B
import eventBus from './eventBus'export default {mounted() {eventBus.on('message', (msg) => {console.log(msg)})},beforeUnmount() {eventBus.off('message')}
}

组件通信最佳实践

1. 父子组件通信

// 推荐使用 props/emit
// 父组件
<template><child-componentv-model="value":config="config"@update="handleUpdate"/>
</template>// 子组件
<script>
export default {props: {modelValue: String,config: Object},emits: ['update:modelValue', 'update'],methods: {updateValue(newValue) {this.$emit('update:modelValue', newValue)}}
}
</script>

2. 跨组件通信

// 推荐使用 Pinia
import { defineStore } from 'pinia'export const useUserStore = defineStore('user', {state: () => ({userInfo: null,}),actions: {async fetchUserInfo() {const data = await api.getUserInfo()this.userInfo = data},},
})

常见问题

1. Props 单向数据流

// 错误示例
export default {props: {message: String},methods: {updateMessage() {this.message = 'New Message' // 错误:直接修改 props}}
}// 正确示例
export default {props: {message: String},methods: {updateMessage() {this.$emit('update:message', 'New Message')}}
}

2. 组件解耦

// 使用组合式函数
function useCounter() {const count = ref(0)const increment = () => count.value++const decrement = () => count.value--return {count,increment,decrement,}
}

面试题

  1. Vue 组件间有哪些通信方式?
// 答案要点:
// 1. props/emit:父子组件通信
// 2. provide/inject:跨层级通信
// 3. Vuex/Pinia:状态管理
// 4. EventBus:事件总线(不推荐)
// 5. $parent/$children:直接访问
// 6. $refs:组件引用
  1. 为什么 Vue 不推荐使用 EventBus?
// 答案要点:
// 1. 难以追踪数据流向
// 2. 可能导致内存泄漏
// 3. 不利于代码维护
// 4. 难以调试
  1. Vue3 中如何实现组件通信?
// 答案要点:
// 1. 使用 defineProps 和 defineEmits
// 2. 使用 provide/inject
// 3. 使用 Pinia 进行状态管理
// 4. 使用组合式函数实现逻辑复用
http://www.mmbaike.com/news/22203.html

相关文章:

  • 我的世界怎么自己做皮肤并上传网站旺道seo优化软件
  • 宁波做网站的大公司产品软文案例
  • 网站开发人员岗位职责nba中国官方网站
  • 肇庆有哪家做企业网站的南京百度推广
  • 海外网站代理如何获取永久免费域名
  • 最好的素材网站南和网站seo
  • 三九集团如何进行网站建设seo整站优化推广
  • php 深圳 电子商务网站开发sem是什么测试
  • 那家建设网站p2p公司最好?永久免费自助建站软件
  • 在网站做电子画册市场调研的方法
  • 网站建设好友鄂尔多斯seo
  • 网站备案收费吗竞价恶意点击犯法吗
  • 校园二手网站的建设方案有道搜索
  • 找人做方案的网站广州网站设计公司
  • 外国专门做魔兽世界邪恶补丁网站绍兴seo推广公司
  • 旅行社手机网站建设重庆seo网站运营
  • 网站的建设维护及管理制度怎么做好网络销售
  • 上海做兼职网站有吗企业营销策划书范文
  • 三河网站建设百度广告商
  • 做鞋子批发网站有哪些网页模板下载
  • 软件设计培训高级seo是什么职位
  • 给彩票网站做排名违法吗自媒体服务平台
  • 汉南做网站合肥seo招聘
  • 做网站域名费一般多少钱专业关键词排名软件
  • 如何制作自己的网站链接教程搜索推广开户
  • 美女做暖暖的视频网站镇江优化推广
  • 河南省建设厅网站 吴浩seo 服务
  • 浙江省建设协会网站深圳网络推广公司
  • 深圳宝安区有几个镇泉州百度推广排名优化
  • 重庆建网站 私单盘多多网盘资源库