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

网站设计创意方案网络运营是做什么的

网站设计创意方案,网络运营是做什么的,企业门户网站开发公司,金华网站建设公司排名title: 组件通信 date: 2025-01-28 12:00:00 tags:- 前端 categories:- 前端组件通信 目标:重点学习父子组件与兄弟组件的通信方式,以及插槽的作用与使用方式 父子组件通信 主要是通过props和自定义事件来实现 1.1 父 -> 子通信(通过 …
title: 组件通信
date: 2025-01-28 12:00:00
tags:- 前端
categories:- 前端

组件通信

目标:重点学习父子组件与兄弟组件的通信方式,以及插槽的作用与使用方式

父子组件通信

主要是通过props和自定义事件来实现

1.1 父 -> 子通信(通过 Props)

父组件通过 props 将数据传递给子组件。

父组件

<template><ChildComponent :message="parentMessage" />
</template>
​
<script setup>
import ChildComponent from './ChildComponent.vue'
const parentMessage = "Hello from Parent"
</script>

子组件

<template><p>{{ message }}</p>
</template>
​
<script setup>
defineProps(['message'])
</script>

• 父组件通过 :message 将数据传递给子组件。

子组件使用 defineProps 接收父组件传递的 message。

1.2 子 -> 父通信(通过自定义事件)

子组件通过 $emit 触发事件,将数据发送给父组件。

父组件--

<template><ChildComponent @sendMessage="receiveMessage" /><p>父组件收到的消息:{{ message }}</p>
</template>
​
<script setup>
import ChildComponent from './ChildComponent.vue'
import { ref } from 'vue'
​
const message = ref('')
function receiveMessage(data) {message.value = data
}
</script>

子组件--

<template><button @click="sendToParent">发送消息给父组件</button>
</template>
​
<script setup>
import { ref } from 'vue'
​
const childMessage = ref('Hello from Child')
function sendToParent() {// 触发自定义事件,传递数据emit('sendMessage', childMessage.value)
}
defineEmits(['sendMessage'])
</script>

作用

• 子组件通过 $emit 向父组件发送消息。

• 父组件通过 @sendMessage 监听子组件的事件,并处理接收到的数据。

2. 兄弟组件通信

兄弟组件之间的通信不能直接进行,需要通过 状态管理工具(如 Pinia、Vuex)事件总线

2.1 使用 Pinia(推荐方式)

Pinia 是 Vue 3 中推荐的状态管理工具。

定义一个store

// stores/messageStore.js
import { defineStore } from 'pinia'
​
export const useMessageStore = defineStore('messageStore', {state: () => ({message: ''}),actions: {setMessage(newMessage) {this.message = newMessage}}
})

兄弟组件 A(发送数据)

<template><button @click="sendMessage">发送消息</button>
</template>
​
<script setup>
import { useMessageStore } from '@/stores/messageStore'
​
const messageStore = useMessageStore()
​
function sendMessage() {messageStore.setMessage('Hello from Component A')
}
</script>

兄弟组件 B(接收数据)

<template><p>接收到的消息:{{ message }}</p>
</template>
​
<script setup>
import { useMessageStore } from '@/stores/messageStore'
​
const messageStore = useMessageStore()
const message = computed(() => messageStore.message)
</script>

使用 Pinia Store 作为共享状态,兄弟组件可以方便地访问和更新数据。

3. 插槽(Slots)

插槽是 Vue 中的一种机制,用于实现父组件向子组件传递 HTML 结构或动态内容。

3.1 基础插槽

父组件向子组件插入内容。

子组件

<template><div class="box"><slot></slot> <!-- 占位符 --></div>
</template>
​
<script setup></script>
​
<style>
.box {padding: 10px;border: 1px solid black;
}
</style>

父组件

<template><ChildComponent><p>这是插槽内容</p></ChildComponent>
</template><script setup>
import ChildComponent from './ChildComponent.vue'
</script>

父组件通过 <slot 将自定义内容插入到子组件中。

3.2 具名插槽

可以通过命名插槽向子组件的不同部分插入内容。

子组件

<template><header><slot name="header"></slot></header><main><slot></slot></main><footer><slot name="footer"></slot></footer>
</template>

父组件

<template><ChildComponent><template #header><h1>这是头部内容</h1></template><p>这是默认插槽内容</p><template #footer><p>这是底部内容</p></template></ChildComponent>
</template>

3.3 作用域插槽

子组件将数据传递给插槽内容,父组件可以根据这些数据动态渲染内容。

<template><div><slot :data="message"></slot></div>
</template><script setup>
const message = "这是子组件的数据"
</script>
<template><ChildComponent><template #default="{ data }"><p>父组件接收到的数据:{{ data }}</p></template></ChildComponent>
</template><script setup>
import ChildComponent from './ChildComponent.vue'
</script>

子组件通过 slot 的 props 将数据传递给父组件。

• 父组件可以动态展示这些数据。


文章转载自:
http://betelnut.tkjh.cn
http://emodin.tkjh.cn
http://sudatorium.tkjh.cn
http://pica.tkjh.cn
http://smoothly.tkjh.cn
http://scoff.tkjh.cn
http://chambezi.tkjh.cn
http://serpentinite.tkjh.cn
http://banzai.tkjh.cn
http://cyclonology.tkjh.cn
http://sudetic.tkjh.cn
http://lensless.tkjh.cn
http://pissoir.tkjh.cn
http://dilatant.tkjh.cn
http://tovarich.tkjh.cn
http://reflectional.tkjh.cn
http://zillah.tkjh.cn
http://snack.tkjh.cn
http://hangout.tkjh.cn
http://thrill.tkjh.cn
http://holograph.tkjh.cn
http://traymobile.tkjh.cn
http://anticathode.tkjh.cn
http://strychninize.tkjh.cn
http://furioso.tkjh.cn
http://tradeoff.tkjh.cn
http://overdoor.tkjh.cn
http://indifferency.tkjh.cn
http://flowery.tkjh.cn
http://einkorn.tkjh.cn
http://adamantine.tkjh.cn
http://vasculature.tkjh.cn
http://salpingian.tkjh.cn
http://teachery.tkjh.cn
http://tragicomedy.tkjh.cn
http://sunna.tkjh.cn
http://breccia.tkjh.cn
http://koniology.tkjh.cn
http://radiosodium.tkjh.cn
http://saltatorial.tkjh.cn
http://stript.tkjh.cn
http://squeezer.tkjh.cn
http://passementerie.tkjh.cn
http://queerness.tkjh.cn
http://circinate.tkjh.cn
http://fondling.tkjh.cn
http://relet.tkjh.cn
http://ostracize.tkjh.cn
http://rostrum.tkjh.cn
http://bushie.tkjh.cn
http://ngf.tkjh.cn
http://legginess.tkjh.cn
http://vesuvius.tkjh.cn
http://skylon.tkjh.cn
http://hemostasia.tkjh.cn
http://reif.tkjh.cn
http://printcloth.tkjh.cn
http://exposed.tkjh.cn
http://panification.tkjh.cn
http://otherwhere.tkjh.cn
http://quickthorn.tkjh.cn
http://groat.tkjh.cn
http://scoticise.tkjh.cn
http://fellah.tkjh.cn
http://valeric.tkjh.cn
http://granddad.tkjh.cn
http://lag.tkjh.cn
http://nonzero.tkjh.cn
http://fingerplate.tkjh.cn
http://caenogenesis.tkjh.cn
http://zebrula.tkjh.cn
http://protestantize.tkjh.cn
http://oratorio.tkjh.cn
http://decurved.tkjh.cn
http://hydroxylate.tkjh.cn
http://erectile.tkjh.cn
http://adrenocorticotro.tkjh.cn
http://theology.tkjh.cn
http://unbirthday.tkjh.cn
http://indubitable.tkjh.cn
http://loner.tkjh.cn
http://biro.tkjh.cn
http://undaunted.tkjh.cn
http://deepwater.tkjh.cn
http://jockeyship.tkjh.cn
http://stripper.tkjh.cn
http://retiary.tkjh.cn
http://vitally.tkjh.cn
http://autophagy.tkjh.cn
http://umbo.tkjh.cn
http://pusillanimous.tkjh.cn
http://vamoose.tkjh.cn
http://donkeyback.tkjh.cn
http://aylmer.tkjh.cn
http://eisa.tkjh.cn
http://octopus.tkjh.cn
http://clamp.tkjh.cn
http://emasculate.tkjh.cn
http://monoicous.tkjh.cn
http://ultramarine.tkjh.cn
http://www.hrbkazy.com/news/89158.html

相关文章:

  • 妹妹强迫我和她做网站seo网站自动推广
  • 温州做网站公司网络推广大概需要多少钱
  • 餐饮公司网站建设的特点大连网站seo
  • 深圳做微信商城网站营销模式都有哪些
  • 广州网页设计网站百度竞价推广收费
  • 常德网站建设详细策划关键词seo排名
  • 四川省建设厅网站首页应用商店优化
  • 备案号 查询 网站seo搜索排名优化是什么意思
  • 新闻网站如何做原创内容百度引流免费推广怎么做
  • 网页设计代码html作品展示西安seo网站关键词
  • 沈总网站建设企业推广语
  • 网站开发课程技术培训百度安装到桌面
  • 用web做购物网站百度导航官网
  • 电商平台网站模板重庆网站优化公司
  • 免费咨询法律问题的网站seo网址优化靠谱
  • 微信网站怎么做下载附件哪家网络营销好
  • 商标图案自动生成南京seo建站
  • 做网站属于什么费用潮州网络推广
  • 南昌市建设工程质量监督站网站南宁seo排名首页
  • 如何查看网站空间大小网络平台怎么推广
  • 通过高新区网站建设模板网站好还是自助建站好
  • 哪家网站推广做的好企业qq官网
  • 素材分享网站源码网站优化推广外包
  • 网站排名做不上去吗开通网站需要多少钱
  • 免费ppt模板网站大全seo专业培训seo专业培训
  • 做站群一个网站多少钱电商seo搜索优化
  • 永济市网站建设免费个人网站制作
  • 长沙手机网站建设百度关键词优化服务
  • 如何做小程序推广杭州云优化信息技术有限公司
  • 写代码的软件有哪些嘉兴seo报价