上一篇
🔹 全局组件
Vue.component('my-alert', { template: `<div class="alert">🚨 {{ message }}</div>`, props: ['message'] })
🔹 局部组件
new Vue({ components: { 'user-card': { template: `<div>👤 {{ user.name }}</div>`, props: ['user'] } } })
🔹 动态组件
<component :is="currentComponent"></component>
场景 | 方法 | 示例 |
---|---|---|
父子传参 | Props向下传递 | <child :data="parentData"> |
子向父通信 | $emit触发事件 | this.$emit('custom-event', data) |
跨层级通信 | EventBus/Vuex | bus.$emit('global-event', data) |
🔹 Vue2实现(Object.defineProperty)
function defineReactive(obj, key) { const dep = new Dep(); Object.defineProperty(obj, key, { get() { dep.depend(); // 收集依赖 return val; }, set(newVal) { dep.notify(); // 触发更新 } }); }
🔹 Vue3改进(Proxy)
function reactive(obj) { return new Proxy(obj, { get(target, key) { track(target, key); // 追踪依赖 return target[key]; }, set(target, key, val) { target[key] = val; trigger(target, key); // 触发更新 return true; } }); }
🔸 对象属性增删
// Vue2需要使用特殊API this.$set(this.obj, 'newProp', 'value');
🔸 数组变更检测
// 只能检测方法调用 this.arr.push(newItem); // ✅ 有效 this.arr[0] = 'newVal'; // ❌ 无效
<!-- 商品列表组件 --> <product-list :products="products"> <template v-slot:default="{ product }"> <product-card :product="product" @add-to-cart="handleAdd"/> </template> </product-list>
// 状态管理(Vuex示例) const store = new Vuex.Store({ state: { cart: [] }, mutations: { ADD_TO_CART(state, product) { state.cart.push(product); } } });
类型 | 推荐资源 | 特点 |
---|---|---|
官方文档 | Vue2中文文档 | 权威全面,持续更新 📖 |
实战课程 | Vue2实战案例 | 电商系统全流程解析 🛒 |
组件库 | Element UI / iView | 企业级UI组件 🎨 |
📌 :掌握Vue2的组件化和响应式系统,就像拥有了构建现代Web应用的瑞士军刀,持续关注生态发展,让你的技术栈永远保持锋利! 🔪✨
本文由 业务大全 于2025-08-26发表在【云服务器提供商】,文中图片由(业务大全)上传,本平台仅提供信息存储服务;作者观点、意见不代表本站立场,如有侵权,请联系我们删除;若有图片侵权,请您准备原始证明材料和公证书后联系我方删除!
本文链接:https://xdh.7tqx.com/wenda/736285.html
发表评论