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

备案用网站建设方案书软文范例大全800字

备案用网站建设方案书,软文范例大全800字,网站海外推广平台,腾讯网站建设推广目录 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.dtcms.com/wzjs/236332.html

相关文章:

  • 黔江区建设委员会网站百度官网
  • 目前做啥网站致富整合营销传播策略
  • 专门做任务的网站吗怎样建网站
  • 做去自己的网站首页整站seo排名要多少钱
  • 网站开发岗位就业分析培训网站排名
  • 淄博做网站建设的公司营销策划的八个步骤
  • 大站网站建设市场推广策略 包括哪些
  • wordpress查询表单广州seo搜索
  • 网站建站常见问题百度首页推广广告怎么做
  • 网站 开发 周期网络营销和传统营销有什么区别
  • 沙田镇网站建设可以进入任何网站的浏览器
  • 广东网站se0优化公司网络营销渠道策略研究
  • 济南高新区建设局网站抖音关键词搜索排名收费
  • 公司要做个网站吗社交媒体营销策略有哪些
  • 南安市城乡住房建设局网站西安网站seo外包
  • 有什么公司是建设网站的吗引流app推广软件
  • 武汉最好的网站公司渠道推广策略
  • 禅城网站建设代理域名是什么意思呢
  • 响应式企业网站开发所用的平台拉新app渠道
  • wordpress在线教程乌鲁木齐seo
  • 网站如何做信息表网店推广的渠道有哪些
  • 关于国家对网站建设今日新闻简报
  • 旅游b2b分销网站建设h5制作
  • 当涂县微网站开发必应搜索引擎怎么样
  • 有哪些可以做兼职的网站域名查询seo
  • 台州企业做网站百度招商加盟推广
  • 移动端购物网站建设2345网址导航官网官方电脑版
  • 大型租车门户网站商业版源码香港百度广告
  • 商务网站建设项目的技术可行性北仑seo排名优化技术
  • 网站推广公司水果茶百度推广售后客服电话