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

中宁网站建设bing搜索 国内版

中宁网站建设,bing搜索 国内版,专业的餐饮网站建设,教育培训机构微网站模板在 Vue3 中,组件之间的通信是构建应用程序的关键 1. 父组件向子组件传递数据 (Props)「父组件:」「子组件:」 2. 子组件向父组件传递数据 (Emit)「父组件:」「子组件:」 3. 兄弟组件通信 (Mitt)「发送事件的组件:」「接收事件的组件:」 4. 透传 Attributes ($attrs)「父组件:」…

在 Vue3 中,组件之间的通信是构建应用程序的关键

  • 1. 父组件向子组件传递数据 (Props)
    • 「父组件:」
    • 「子组件:」
  • 2. 子组件向父组件传递数据 (Emit)
    • 「父组件:」
    • 「子组件:」
  • 3. 兄弟组件通信 (Mitt)
    • 「发送事件的组件:」
    • 「接收事件的组件:」
  • 4. 透传 Attributes ($attrs)
    • 「父组件:」
    • 「子组件:」
  • 5. 模板引用 (Refs)
    • 「父组件:」
    • 「子组件:」
  • 6. 双向绑定 (v-model)
    • 「父组件:」
    • 「子组件:」
  • 7. 依赖注入 (Provide/Inject)
    • 「祖先组件:」
    • 「子孙组件:」
  • 8. 路由传参
    • 「通过 query 传参:」
    • 「通过 params 传参:」
    • 「通过 router-link 里面的 to 传参:」
  • 9. Vuex 状态管理
    • store/index.js
    • 组件中使用
  • 10. Pinia 状态管理
    • store.js
    • 组件中使用
  • 11. 浏览器存储
  • 12. Window 对象
  • 13. 全局属性
  • 总结:

1. 父组件向子组件传递数据 (Props)

这是最基本也是最常用的通信方式。父组件通过属性向子组件传递数据。

「父组件:」

<template><child :name="name"></child>
</template><script setup>
import { ref } from 'vue'
import Child from './Child.vue'
const name = ref('小明')
</script>

「子组件:」

<template><div>{{ props.name }}</div>
</template><script setup>
const props = defineProps({name: {type: String,default: '',},
})
</script>

2. 子组件向父组件传递数据 (Emit)

子组件可以通过触发事件的方式向父组件传递数据。

「父组件:」

<template><child @greet="handleGreet"></child>
</template><script setup>
import { ref } from 'vue'
import Child from './Child.vue'
const handleGreet = (message) => {console.log(message) // 输出: "来自子组件的问候"
}
</script>

「子组件:」

<template><button @click="handleClick">点击我</button>
</template><script setup>
import { ref, defineEmits } from 'vue'
const message = ref('来自子组件的问候')
const emits = defineEmits(['greet'])
const handleClick = () => {emits('greet', message.value)
}
</script>

3. 兄弟组件通信 (Mitt)

对于兄弟组件之间的通信,我们可以使用第三方库 mitt 来实现一个简单的事件总线。首先,安装 mitt:

npm install --save mitt

然后,在 main.js 中全局配置:

import { createApp } from 'vue'
import mitt from 'mitt'
import App from './App.vue'
const app = createApp(App)
app.config.globalProperties.$bus = mitt()
app.mount('#app')

「发送事件的组件:」

<script setup>
import { getCurrentInstance } from 'vue'
const { proxy } = getCurrentInstance()
const sendMessage = () => {proxy.$bus.emit('myEvent', '你好,兄弟')
}
</script>

「接收事件的组件:」

<script setup>
import { onMounted, getCurrentInstance } from 'vue'
const { proxy } = getCurrentInstance()
onMounted(() => {proxy.$bus.on('myEvent', (message) => {console.log(message) // 输出: "你好,兄弟"})
})
</script>

4. 透传 Attributes ($attrs)

$attrs 包含了父组件传递给子组件的所有属性,除了那些已经被 props 或 emits 声明的。

「父组件:」

<template><child name="小明" age="18" hobby="篮球"></child>
</template>

「子组件:」

<script setup>
import { useAttrs } from 'vue'
const attrs = useAttrs()
console.log(attrs) // { age: "18", hobby: "篮球" }
</script>

5. 模板引用 (Refs)

通过 ref,父组件可以直接访问子组件的属性和方法。

「父组件:」

<template><child ref="childRef"></child><button @click="callChildMethod">调用子组件方法</button>
</template><script setup>
import { ref } from 'vue'
import Child from './Child.vue'
const childRef = ref(null)
const callChildMethod = () => {childRef.value.someMethod()
}
</script>

「子组件:」

<script setup>
import { defineExpose } from 'vue'
const someMethod = () => {console.log('子组件方法被调用了')
}
defineExpose({someMethod,
})
</script>

6. 双向绑定 (v-model)

v-model 提供了一种简洁的方式来实现父子组件之间的双向数据绑定。

「父组件:」

<template><child v-model:name="name"></child>
</template><script setup>
import { ref } from 'vue'
import Child from './Child.vue'
const name = ref('小明')
</script>

「子组件:」

<template><input :value="name" @input="updateName" />
</template><script setup>
import { defineProps, defineEmits } from 'vue'
const props = defineProps(['name'])
const emit = defineEmits(['update:name'])
const updateName = (e) => {emit('update:name', e.target.value)
}
</script>

7. 依赖注入 (Provide/Inject)

provide 和 inject 允许祖先组件向所有子孙组件传递数据,而不需要通过每一层组件手动传递。

「祖先组件:」

<script setup>
import { provide, ref } from 'vue'
const themeColor = ref('blue')
provide('theme', themeColor)
</script>

「子孙组件:」

<script setup>
import { inject } from 'vue'
const theme = inject('theme')
console.log(theme.value) // 'blue'
</script>

8. 路由传参

Vue Router 提供了多种方式在路由之间传递参数。

「通过 query 传参:」

import { useRouter } from 'vue-router'
const router = useRouter()
router.push({ path: '/user', query: { id: 123 } })// 在目标组件中
import { useRoute } from 'vue-router'
const route = useRoute()
console.log(route.query.id) // 123

「通过 params 传参:」

同上:query=>params

「通过 router-link 里面的 to 传参:」

<router-link to="/father/son/传入的参数">父亲组件<router-link>
<router-link to="path: '/father/son', query: { title: title }">父亲组<router-link>

9. Vuex 状态管理

Vuex 是 Vue 的官方状态管理库,适用于大型应用。

store/index.js

import { createStore } from 'vuex'
export default createStore({state: {count: 0,},mutations: {increment(state) {state.count++},},
})

组件中使用

import { useStore } from 'vuex'
const store = useStore()
console.log(store.state.count)
store.commit('increment')

10. Pinia 状态管理

Pinia 是新一代的 Vue 状态管理库,提供更简单的 API 和更好的 TypeScript 支持。

store.js

import { defineStore } from 'pinia'export const useCounterStore = defineStore('counter', {state: () => ({ count: 0 }),actions: {increment() {this.count++},},
})

组件中使用

import { useCounterStore } from '@/store'
const counter = useCounterStore()
console.log(counter.count)
counter.increment()

11. 浏览器存储

localStorage 和 sessionStorage 可以用于在不同页面或组件之间共享数据。

// localStorage存储数据
localStorage.setItem('user', JSON.stringify({ name: '小明', age: 18 }))
// localStorage读取数据
const user = JSON.parse(localStorage.getItem('user'))// sessionStorage 存储数据
sessionStorage .setItem('user', JSON.stringify({ name: '小明', age: 18 }))
// sessionStorage 读取数据
const user = JSON.parse(sessionStorage .getItem('user'))

12. Window 对象

虽然不推荐,但在某些场景下,可以使用 window 对象在全局范围内共享数据。

// 设置全局数据
window.globalData = { message: '全局消息' }// 在任何地方使用
console.log(window.globalData.message)

13. 全局属性

Vue 3 提供了 app.config.globalProperties 来替代 Vue 2 中的 Vue.prototype,用于添加全局可用的属性。

// main.js
const app = createApp(App)
app.config.globalProperties.$http = axios// 在组件中使用
import { getCurrentInstance } from 'vue'
const { proxy } = getCurrentInstance()
proxy.$http.get('/api/data')

总结:

总结这 13 种方法涵盖了 Vue 3 中几乎所有的组件通信场景。根据你的具体需求和应用规模,选择最合适的通信方式。好的组件设计能够简化通信,提高代码的可维护性。

http://www.khdw.cn/news/69235.html

相关文章:

  • 网站优化推广软件网络营销策划书范文
  • 百度站长工具网址永久开源的免费建站系统
  • .net 快速网站开发软文写作兼职
  • 火车头web发布到网站服务营销的七个要素
  • 岳麓做网站的公司百度收录情况
  • 各大中文网站搜索引擎调价工具哪个好
  • 建设银行官方网站首页公司机构线上网络推广怎么做
  • 文章收录网站seo优化啥意思
  • wordpress 图片加载很慢网络优化器
  • wordpress能做流量站吗seo行业岗位有哪些
  • 如何用ps做创意视频网站怎么在百度上推广产品
  • 聊城做网站的公司行情百度竞价托管一月多少钱
  • 兰州市建设厅网站seo搜索引擎优化课程总结
  • 做 直销网站 公司如何用html制作网页
  • 网站建设需求分析文档网站建设公司是怎么找客户
  • 培训机构网站源码seo优化工具大全
  • 网站建设及发展seo排名优化工具
  • 网站怎么做自营销网络营销推广有哪些方法
  • 蛋糕店网站设计模板网站快速上排名方法
  • 云南专业网站建设个人网页生成器
  • 花店网站建设构思免费域名注册
  • 那些网站做推广简述企业网站推广的一般策略
  • 网站建设公司怎么做业务网络营销网
  • 浙江嘉兴建设局网站seo工具网站
  • 工作室 网站经营性备案郑州网站
  • 最近几天的重大新闻事件淘宝网站的推广与优化
  • 少儿编程官网北京seo招聘信息
  • 百度新网站收录济南seo外包服务
  • 大山子网站建设百度公司官网入口
  • 模板网站与定制网站区别谷歌浏览器 安卓下载2023版