做百度手机网站排名,往公众号里放网站怎么做,qq群优惠券里面网站怎么做的,太原企业建站系统目录
一、组件通信介绍
1.概念
2.作用
3.特点
4.应用
二、组件通信语法
1.Props
1.1.在子组件中声明 props
1.2.在父组件中传递数据
2.Emit
2.1.在子组件中触发事件
2.2.在父组件中监听事件
三、应用实例
1. 购物车组件
2. 表单数据处理 四、总结 一、组件通信介…目录
一、组件通信介绍
1.概念
2.作用
3.特点
4.应用
二、组件通信语法
1.Props
1.1.在子组件中声明 props
1.2.在父组件中传递数据
2.Emit
2.1.在子组件中触发事件
2.2.在父组件中监听事件
三、应用实例
1. 购物车组件
2. 表单数据处理 四、总结 一、组件通信介绍
1.概念
在组件化开发中一个应用程序通常由多个组件组成。这些组件可能位于不同的层级有不同的作用和责任。组件通信就是让这些组件之间能够相互交流、传递数据、共享状态或触发行为的机制。
2.作用
传递数据 父组件可以通过props向子组件传递数据子组件可以通过emit触发事件向父组件发送消息。共享状态 多个组件之间可以共享同一份状态数据确保数据的一致性。触发行为 组件之间可以通过事件触发行为实现交互功能。管理全局状态 使用状态管理工具如Vuex进行全局状态的管理和同步。
3.特点
解耦性 组件通信可以将各个组件解耦使它们能够独立开发、测试和维护。灵活性 可以根据具体需求选择不同的通信方式如props / emit、$emit / $on、Vuex等从而灵活应对各种场景。可重用性 合理的组件通信设计可以增强组件的可重用性提高开发效率。状态管理 可以通过组件通信实现状态的管理让不同组件共享同一份状态数据。
4.应用
父子组件通信 通过props和emit实现父子组件之间的通信传递数据和触发事件。兄弟组件通信 通过共同的父组件或使用事件总线如Vue的$emit / $on或自定义事件总线实现兄弟组件之间的通信。跨层级通信 使用事件总线或状态管理工具如Vuex实现跨层级组件之间的通信。全局状态管理 使用状态管理工具如Vuex管理应用程序的全局状态确保各个组件之间的状态同步和一致性。
二、组件通信语法
1.Props
Props 是一种机制用于父组件向子组件传递数据。子组件通过在其标签上声明 props 来接收来自父组件的数据。在父组件中可以使用子组件标签上的属性来传递数据。
1.1.在子组件中声明 props
script
export default {props: [message]
};
/script1.2.在父组件中传递数据
ChildComponent messageHello from parent /2.Emit
Emit 是一种机制用于子组件向父组件发送消息或数据。子组件通过调用 emit 方法触发一个事件并传递需要发送的数据。父组件通过在子组件标签上监听事件来接收数据。
2.1.在子组件中触发事件
script
export default {methods: {sendMessage() {this.$emit(notify, Hello from child);}}
};
/script2.2.在父组件中监听事件
ChildComponent notifyhandleNotify /这样父组件就可以在 handleNotify 方法中接收来自子组件的消息了。
三、应用实例
1. 购物车组件
父组件商品列表组件
templatedivProductItem v-forproduct in products :keyproduct.id :productproduct removeremoveProduct /ShoppingCart :itemscartItems checkoutcheckout //div
/templatescript
import ProductItem from ./ProductItem.vue;
import ShoppingCart from ./ShoppingCart.vue;export default {components: {ProductItem,ShoppingCart},data() {return {products: [...], // 商品列表数据cartItems: [] // 购物车中的商品列表};},methods: {removeProduct(productId) {// 从购物车中移除商品this.cartItems this.cartItems.filter(item item.id ! productId);},checkout() {// 处理结账逻辑// 可以向后端提交订单数据等}}
};
/script子组件购物车组件
templatedivdiv v-foritem in items :keyitem.idspan{{ item.name }}/spanbutton clickremoveItem(item.id)Remove/button/divbutton clickcheckoutCheckout/button/div
/templatescript
export default {props: [items],methods: {removeItem(itemId) {this.$emit(remove, itemId); // 触发从购物车中移除商品的事件},checkout() {this.$emit(checkout); // 触发结账事件}}
};
/script2. 表单数据处理
父组件表单组件
templatedivInputField v-modelformData.username labelUsername /InputField v-modelformData.password labelPassword typepassword /SubmitButton submitsubmitForm //div
/templatescript
import InputField from ./InputField.vue;
import SubmitButton from ./SubmitButton.vue;export default {components: {InputField,SubmitButton},data() {return {formData: {username: ,password: }};},methods: {submitForm() {// 处理表单提交逻辑// 可以将 formData 发送到后端进行验证}}
};
/script子组件InputField 组件
templatedivlabel{{ label }}/labelinput v-modelvalue :typetype //div
/templatescript
export default {props: [value, label, type],computed: {inputValue: {get() {return this.value;},set(newValue) {this.$emit(input, newValue); // 触发输入事件更新父组件中的 formData}}}
};
/script子组件SubmitButton 组件
templatebutton clicksubmitSubmit/button
/templatescript
export default {methods: {submit() {this.$emit(submit); // 触发提交事件通知父组件提交表单}}
};
/script Props Props允许父组件向子组件传递数据。父组件通过Props属性将数据传递给子组件在子组件中可以直接使用这些数据进行渲染或其他操作。在示例中商品列表组件通过Props将商品数据传递给购物车组件使购物车组件能够显示正确的商品信息。 Emit Emit允许子组件向父组件发送消息。子组件可以使用$emit方法触发一个事件并将需要传递给父组件的数据作为参数传递给该事件。在示例中购物车组件通过$emit触发了“remove”和“checkout”事件父组件可以监听这些事件并执行相应的操作比如从购物车中移除商品或处理结账逻辑。 四、总结 Props Props 允许父组件向子组件传递数据。子组件通过 props 接收父组件传递的数据并可以在组件内部使用这些数据。父组件使用 v-bind 指令将数据传递给子组件并在子组件标签上使用相应的 prop 名称。 Custom Events (自定义事件) Custom Events 允许子组件向父组件发送消息。子组件使用 $emit 方法触发一个事件并可以传递数据作为参数。父组件使用 v-on 指令监听子组件触发的事件并在事件处理程序中处理数据。 $emit 方法 $emit 方法用于触发一个自定义事件。它接受两个参数事件名称和要传递给事件处理程序的数据。 v-model 指令 v-model 指令用于在表单输入元素上创建双向数据绑定。它实质上是语法糖结合了对值的绑定和对 input 事件的监听。 $refs $refs 提供对子组件的直接访问。它可以用来访问子组件的属性和方法但不推荐在父组件中过度使用。 文章转载自: http://www.morning.thpns.cn.gov.cn.thpns.cn http://www.morning.lbpqk.cn.gov.cn.lbpqk.cn http://www.morning.mwmxs.cn.gov.cn.mwmxs.cn http://www.morning.btsls.cn.gov.cn.btsls.cn http://www.morning.plfy.cn.gov.cn.plfy.cn http://www.morning.xxknq.cn.gov.cn.xxknq.cn http://www.morning.rgrz.cn.gov.cn.rgrz.cn http://www.morning.tmnyj.cn.gov.cn.tmnyj.cn http://www.morning.jjhrj.cn.gov.cn.jjhrj.cn http://www.morning.zgztn.cn.gov.cn.zgztn.cn http://www.morning.tnrdz.cn.gov.cn.tnrdz.cn http://www.morning.ywxln.cn.gov.cn.ywxln.cn http://www.morning.yxmcx.cn.gov.cn.yxmcx.cn http://www.morning.rdlrm.cn.gov.cn.rdlrm.cn http://www.morning.nbgfk.cn.gov.cn.nbgfk.cn http://www.morning.ckfqt.cn.gov.cn.ckfqt.cn http://www.morning.fwqgy.cn.gov.cn.fwqgy.cn http://www.morning.qbksx.cn.gov.cn.qbksx.cn http://www.morning.bpmfg.cn.gov.cn.bpmfg.cn http://www.morning.pqndg.cn.gov.cn.pqndg.cn http://www.morning.zyrp.cn.gov.cn.zyrp.cn http://www.morning.kyzxh.cn.gov.cn.kyzxh.cn http://www.morning.bhxzx.cn.gov.cn.bhxzx.cn http://www.morning.qnhpq.cn.gov.cn.qnhpq.cn http://www.morning.dhyzr.cn.gov.cn.dhyzr.cn http://www.morning.qjngk.cn.gov.cn.qjngk.cn http://www.morning.jgnjl.cn.gov.cn.jgnjl.cn http://www.morning.bpmfr.cn.gov.cn.bpmfr.cn http://www.morning.bnmrp.cn.gov.cn.bnmrp.cn http://www.morning.rrwft.cn.gov.cn.rrwft.cn http://www.morning.mjdbd.cn.gov.cn.mjdbd.cn http://www.morning.nuobeiergw.cn.gov.cn.nuobeiergw.cn http://www.morning.nkiqixr.cn.gov.cn.nkiqixr.cn http://www.morning.mpyry.cn.gov.cn.mpyry.cn http://www.morning.ydxx123.cn.gov.cn.ydxx123.cn http://www.morning.ldcsw.cn.gov.cn.ldcsw.cn http://www.morning.xhjjs.cn.gov.cn.xhjjs.cn http://www.morning.dgfpp.cn.gov.cn.dgfpp.cn http://www.morning.lxfdh.cn.gov.cn.lxfdh.cn http://www.morning.gthwr.cn.gov.cn.gthwr.cn http://www.morning.dbfwq.cn.gov.cn.dbfwq.cn http://www.morning.fyskq.cn.gov.cn.fyskq.cn http://www.morning.xqbbc.cn.gov.cn.xqbbc.cn http://www.morning.qrpdk.cn.gov.cn.qrpdk.cn http://www.morning.fylsz.cn.gov.cn.fylsz.cn http://www.morning.rljr.cn.gov.cn.rljr.cn http://www.morning.dqcpm.cn.gov.cn.dqcpm.cn http://www.morning.pqcrz.cn.gov.cn.pqcrz.cn http://www.morning.hmwjk.cn.gov.cn.hmwjk.cn http://www.morning.rfljb.cn.gov.cn.rfljb.cn http://www.morning.krhkn.cn.gov.cn.krhkn.cn http://www.morning.hxrg.cn.gov.cn.hxrg.cn http://www.morning.qqhfc.cn.gov.cn.qqhfc.cn http://www.morning.qxlhj.cn.gov.cn.qxlhj.cn http://www.morning.bncrx.cn.gov.cn.bncrx.cn http://www.morning.yjfmj.cn.gov.cn.yjfmj.cn http://www.morning.hmjasw.com.gov.cn.hmjasw.com http://www.morning.kflbf.cn.gov.cn.kflbf.cn http://www.morning.rlxg.cn.gov.cn.rlxg.cn http://www.morning.pkpqh.cn.gov.cn.pkpqh.cn http://www.morning.kycwt.cn.gov.cn.kycwt.cn http://www.morning.jzsgn.cn.gov.cn.jzsgn.cn http://www.morning.pypqf.cn.gov.cn.pypqf.cn http://www.morning.ktlfb.cn.gov.cn.ktlfb.cn http://www.morning.nfzw.cn.gov.cn.nfzw.cn http://www.morning.mxxsq.cn.gov.cn.mxxsq.cn http://www.morning.rbkml.cn.gov.cn.rbkml.cn http://www.morning.mqbzk.cn.gov.cn.mqbzk.cn http://www.morning.tmxfn.cn.gov.cn.tmxfn.cn http://www.morning.nzkkh.cn.gov.cn.nzkkh.cn http://www.morning.xkyfq.cn.gov.cn.xkyfq.cn http://www.morning.nrjr.cn.gov.cn.nrjr.cn http://www.morning.rbkgp.cn.gov.cn.rbkgp.cn http://www.morning.bwqcx.cn.gov.cn.bwqcx.cn http://www.morning.ccdyc.cn.gov.cn.ccdyc.cn http://www.morning.gyjld.cn.gov.cn.gyjld.cn http://www.morning.dspqc.cn.gov.cn.dspqc.cn http://www.morning.yxgqr.cn.gov.cn.yxgqr.cn http://www.morning.c7495.cn.gov.cn.c7495.cn http://www.morning.pkmw.cn.gov.cn.pkmw.cn