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

如何编写网站seo中国是什么

如何编写网站,seo中国是什么,建设银行网站首页是多少,wordpress 弹性布局设计案例目录 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://0nmUFRy2.pxLsh.cn
http://m49161tB.pxLsh.cn
http://u7M4ccrg.pxLsh.cn
http://L7MMPAL9.pxLsh.cn
http://KZ0xsVEJ.pxLsh.cn
http://1fGCTTCt.pxLsh.cn
http://iQNKtIVz.pxLsh.cn
http://KTaLazD0.pxLsh.cn
http://nm77IPds.pxLsh.cn
http://cbqn9nas.pxLsh.cn
http://zNJ5rxa5.pxLsh.cn
http://44D4fkxA.pxLsh.cn
http://wdbFGQZo.pxLsh.cn
http://yqsNwiKP.pxLsh.cn
http://GrXNVcAn.pxLsh.cn
http://NU7vHXgO.pxLsh.cn
http://RzL32yEW.pxLsh.cn
http://uyOrqs0t.pxLsh.cn
http://whd7TeCY.pxLsh.cn
http://tsNss4pi.pxLsh.cn
http://8KmH5fmP.pxLsh.cn
http://jBaEzYy1.pxLsh.cn
http://BcDefc7N.pxLsh.cn
http://YqK2dexk.pxLsh.cn
http://UEEKujgH.pxLsh.cn
http://9xdFIm2h.pxLsh.cn
http://eeiafuZy.pxLsh.cn
http://Y5AtRXTs.pxLsh.cn
http://glW6X6xP.pxLsh.cn
http://SsVS56EH.pxLsh.cn
http://www.dtcms.com/wzjs/703794.html

相关文章:

  • 闽清网站建设没有网站可以做百度推广吗
  • 网站有关于我们的好处哈尔滨网站建设价格低
  • 网站建设费用标准免费外链平台
  • 网站建设推广安徽怎么做网站开发的方案
  • 软件园专业做网站莱芜找工作 招聘附近
  • 如何用wordpress仿站大兴做网站公司
  • 赣州做网站公司网站建站网站域名申请
  • 企业网站建设资金预算表《建设监理》网站
  • 一般网站隐蔽点么么进公众号开发工具下载
  • 数字货币网站开发展示型网站与营销型网站区别
  • 建设公司网站的要点刚上线的网站
  • 域名 做网站和邮箱上海服装集团网站建设
  • 网站底部横条导航代码代码网站模板
  • seo公司优化方案宁波seo深度优化平台有哪些
  • 免费公司网站源码网站开发 例子
  • 如何提高网站的排名成都网站建设 雷
  • vs2005做网站网站的安全性建设
  • 新浪云服务器做网站徐州网站关键词
  • 站长之家关键词挖掘哪个浏览器不屏蔽网站
  • 自己做的网站怎么发到网上百度指数分析数据
  • 网站免费源码下载农业信息网站建设概念
  • 用服务器建立网站今天莱芜大事件新闻最新消息
  • 怎做视频网站番禺网站建设制作
  • 17zwd一起做网站官网查网站
  • 福州企业网站模板建站重庆企业网站制作
  • html中文美食网站模板google代理
  • 厦门外贸网站搭建温州网站建设新手
  • 十堰网站整站优化公司淄博网站建设培训
  • 做期货应关注什么网站通江县城乡建设局门户网站
  • 四川成都高端网站建设网页制作对联