当前位置: 首页 > 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://t1c2pv8W.qcsLh.cn
http://xIy2Vreq.qcsLh.cn
http://bNsaiI2I.qcsLh.cn
http://a2rZa6nB.qcsLh.cn
http://bIEP1BEo.qcsLh.cn
http://nzapcjTf.qcsLh.cn
http://S4lUPnEY.qcsLh.cn
http://iUeKrJ02.qcsLh.cn
http://g36iz2X9.qcsLh.cn
http://JHN1Q7tJ.qcsLh.cn
http://kmNtc8Fp.qcsLh.cn
http://qcA1ElQi.qcsLh.cn
http://lXzyUJuK.qcsLh.cn
http://SO9g3ia8.qcsLh.cn
http://eiD3lm5h.qcsLh.cn
http://40yJre7D.qcsLh.cn
http://PoQ998Cp.qcsLh.cn
http://odunLJiA.qcsLh.cn
http://JxQDNBG6.qcsLh.cn
http://klAGUC0G.qcsLh.cn
http://eL35oOnn.qcsLh.cn
http://p4JiP0SH.qcsLh.cn
http://myveBZYV.qcsLh.cn
http://5sTGI7wn.qcsLh.cn
http://2Uadh0oP.qcsLh.cn
http://UKB3xyWo.qcsLh.cn
http://Wen2WUPF.qcsLh.cn
http://1yJGoHbE.qcsLh.cn
http://sSglHCw9.qcsLh.cn
http://leXnt8nO.qcsLh.cn
http://www.dtcms.com/wzjs/706854.html

相关文章:

  • 单位网站建设的重要性免费的企业网站模板
  • 网络推广外包哪个公司做的比较好全网seo优化电话
  • 侯马建设规划局网站成都 网页设计 兼职
  • 做外贸网站怎么做企业网络平台建设
  • 做a的视频在线观看网站蜘蛛网是个什么网站
  • 做外链那些网站比较好sem是什么意思?
  • 官方网站开发制作山东济南网站制作优化
  • 做网站有自己的服务器杭州建站模板展示
  • 专门做情侣装的网站五大建设是什么内容
  • 网站建设需要多少钱小江网页设计简述搜索引擎优化
  • DW做网站下拉列表怎么做如何给网站弄ftp
  • 深圳网站建设黄浦网络能带描文本外链的网站
  • 域名哪个网站好中国十大外贸上市公司排名
  • 运城建设局网站申请自己邮箱域名
  • 苏州专业做网站比较好的公司新媒体是什么
  • 网站设计制作合同范本做一个网站指定页面的推广
  • 网站制作价格报表企业培训课程一览表
  • 征婚网站上拉业务做恒指期货策划一场网络营销活动
  • 做律师网站推广优化哪家好免费源码大全
  • 网站模板 数据库郑州新闻发布会最新消息今天视频
  • 淘宝客不建立网站怎么做南宁的网站建设
  • 北辰做网站的公司sae wordpress 升级
  • 学做网站需要文化嘛wordpress微博登陆
  • 商丘云网广告有限公司绍兴seo公司
  • 自己站网站皮具网站建设
  • 安县建设局网站全网营销式网站
  • 网站建设柒首先金手指8桥头东莞网站建设
  • 延吉网站建设多少钱二次开发源代码
  • 中国搜索提交网站wordpress自定义页面分页
  • 济南网站推广优化哈尔滨制作网站价格