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

北京所有做招聘类网站建站公司小广告网站

北京所有做招聘类网站建站公司,小广告网站,网站建设的功能有哪些,做网站属于什么职位在 Vue3 中&#xff0c;组件之间的数据传递主要有以下几种方式&#xff0c;适用于不同的场景&#xff1a; 一、父组件向子组件传值&#xff1a;props 1. 子组件定义 props <!-- ChildComponent.vue --> <script setup> // 组合式 API&#xff08;推荐&#xff09…

在 Vue3 中,组件之间的数据传递主要有以下几种方式,适用于不同的场景:


一、父组件向子组件传值:props

1. 子组件定义 props
<!-- ChildComponent.vue -->
<script setup>
// 组合式 API(推荐)
const props = defineProps({title: {type: String,default: '默认标题'},count: {type: Number,required: true}
});
</script><template><div>{{ title }} - {{ count }}</div>
</template>
<!-- 选项式 API -->
<script>
export default {props: {title: String,count: {type: Number,required: true}}
}
</script>
2. 父组件传递数据
<template><ChildComponent :title="'Hello Vue3'" :count="42"/>
</template>

二、子组件向父组件传值:emit 事件

1. 子组件触发事件
<!-- ChildComponent.vue -->
<script setup>
const emit = defineEmits(['updateCount']); // 定义事件function increment() {emit('updateCount', 10); // 触发事件并传递数据
}
</script><template><button @click="increment">增加计数</button>
</template>
2. 父组件监听事件
<template><ChildComponent :count="count"@updateCount="count = $event" <!-- 接收子组件传递的值 -->/>
</template><script setup>
import { ref } from 'vue';
const count = ref(0);
</script>

三、双向绑定:v-model

1. 子组件支持 v-model
<!-- ChildComponent.vue -->
<script setup>
const props = defineProps(['modelValue']);
const emit = defineEmits(['update:modelValue']);function updateValue(newValue) {emit('update:modelValue', newValue);
}
</script><template><input :value="modelValue" @input="updateValue($event.target.value)">
</template>
2. 父组件使用 v-model
<template><ChildComponent v-model="text" /> <!-- 自动同步数据 -->
</template><script setup>
import { ref } from 'vue';
const text = ref('初始值');
</script>

四、跨层级传值:provide / inject

1. 祖先组件提供数据
<!-- AncestorComponent.vue -->
<script setup>
import { provide, ref } from 'vue';const theme = ref('dark');
provide('theme', theme); // 提供数据
</script>
2. 后代组件注入数据
<!-- ChildComponent.vue -->
<script setup>
import { inject } from 'vue';const theme = inject('theme'); // 注入数据
</script><template><div :class="theme">当前主题:{{ theme }}</div>
</template>

五、通过 Context 共享数据(如 Pinia 状态管理)

1. 安装 Pinia
npm install pinia
2. 创建 Store
// stores/counter.js
import { defineStore } from 'pinia';export const useCounterStore = defineStore('counter', {state: () => ({count: 0}),actions: {increment() {this.count++;}}
});
3. 组件中使用 Store
<template><div>Count: {{ counterStore.count }}</div><button @click="counterStore.increment()">+1</button>
</template><script setup>
import { useCounterStore } from '@/stores/counter';const counterStore = useCounterStore();
</script>

总结:不同场景的传值方式

场景方法适用性
父子组件直接通信props + emit父子层级明确,数据流单向
表单输入双向绑定v-model表单类组件(如输入框、下拉框)
跨层级组件通信provide / inject深层次嵌套组件(如全局配置、主题)
复杂应用状态共享Pinia / Vuex多组件共享状态(推荐中大型项目)

推荐实践

  • 优先使用 propsemit 实现父子通信。
  • 简单双向绑定用 v-model,复杂逻辑用事件。
  • 跨层级或全局状态使用 Pinia。
  • 避免过度依赖 provide/inject,可能导致组件耦合。
http://www.dtcms.com/wzjs/359963.html

相关文章:

  • 网页与网站的区别和关系推广网络推广平台
  • 做网站创业怎么样网络营销的种类有哪些
  • 深圳网站制作公司讯息网站建设规划要点详解
  • 山东政府网站信息内容建设郑州网络营销顾问
  • 天津建设网站首页运营培训班学费大概多少
  • 网站怎么icp备案做网站建设公司
  • 装修公司网站怎么做的如何用html制作网页
  • 荔湾区做网站seo首页关键词优化
  • asp.net 做网站好吗广州seo托管
  • 直销网站制作价格开鲁网站seo不用下载
  • 昆明做网站价格廊坊关键词优化平台
  • wordpress约课系统seo优化流程
  • 自已做个网站怎么做竞价托管代运营公司
  • 集团网站设计欣赏黑马培训是正规学校吗
  • 政府网站建设项目采购需求营销型网站开发公司
  • 台州网站建设惠店科技长沙疫情最新情况
  • 做网站网络营销注意网络营销的渠道
  • 朗朗上口的公司名称企业网站的优化建议
  • wordpress居中样式北京seo百科
  • 合肥集团网站建设公众号开发
  • 莱芜建设网站海门网站建设
  • 收费网站建设邯郸网站seo
  • 网站设计培训费用是多少网络推广怎样做
  • 宝洁网站建设评价关键词优化多少钱
  • wordpress调用搜索功能微博关键词排名优化
  • 佛山网站免费制作爱站网注册人查询
  • 简单做网站的价格优化网站的方法有哪些
  • 加强门户网站建设的通知个人自己免费建网站
  • 贵阳优化网站建设宁波seo推广服务电话
  • 揭阳网站建设维护成功营销十大经典案例