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

网络私人定制网站济南网站建设那家好

网络私人定制网站,济南网站建设那家好,wordpress主题游戏cms,网站项目通信方式适用层级数据流向复杂度Props/Emits父子组件单向/双向★☆☆v-model父子组件双向★☆☆Provide/Inject跨层级组件自上而下★★☆事件总线任意组件任意方向★★★Pinia/Vuex全局状态任意方向★★☆Refs模板引用父子组件父→子★☆☆作用域插槽父子组件子→父★★☆Web W…

通信方式

适用层级

数据流向

复杂度

Props/Emits

父子组件

单向/双向

★☆☆

v-model

父子组件

双向

★☆☆

Provide/Inject

跨层级组件

自上而下

★★☆

事件总线

任意组件

任意方向

★★★

Pinia/Vuex

全局状态

任意方向

★★☆

Refs模板引用

父子组件

父→子

★☆☆

作用域插槽

父子组件

子→父

★★☆

Web Workers

跨线程通信

任意方向

★★★★

1. Props/Emits:基础父子通信

适用场景:直接父子组件通信
注意:避免直接修改props,使用emit通知父组件修改

<!-- 父组件 -->
<script setup>
import Child from './Child.vue';
const message = ref('父组件数据');
const handleEmit = (data) => {console.log('子组件传递:', data);
};
</script><template><Child :msg="message" @child-event="handleEmit" />
</template><!-- 子组件 Child.vue -->
<script setup>
const props = defineProps(['msg']);
const emit = defineEmits(['child-event']);const sendToParent = () => {emit('child-event', { time: new Date() });
};
</script><template><div>收到: {{ msg }}</div><button @click="sendToParent">发送事件</button>
</template>
2. v-model 双向绑定升级

优势:替代Vue2的.sync修饰符,语法更简洁
原理:相当于 :modelValue + @update:modelValue

<!-- 父组件 -->
<script setup>
import CustomInput from './CustomInput.vue';
const username = ref('');
</script><template><CustomInput v-model="username" />
</template><!-- 子组件 CustomInput.vue -->
<script setup>
const model = defineModel();
</script><template><input type="text":value="model"@input="model = $event.target.value"/>
</template>
3. Provide/Inject 跨层级通信

适用场景:多级嵌套组件共享数据
注意:避免滥用,复杂场景建议用状态管理

// 祖先组件
<script setup>
import { provide, ref } from 'vue';
const theme = ref('dark');
provide('app-theme', {theme,toggle: () => {theme.value = theme.value === 'dark' ? 'light' : 'dark';}
});
</script>// 任意后代组件
<script setup>
import { inject } from 'vue';
const { theme, toggle } = inject('app-theme');
</script><template><button @click="toggle">当前主题: {{ theme }}</button>
</template>
4. 事件总线替代方案(mitt)

适用场景:非父子组件通信
优势:轻量级(仅200B),替代Vue2的$emit/$on

// eventBus.js
import mitt from 'mitt';
export default mitt();// 组件A(发布事件)
import bus from './eventBus';
bus.emit('user-login', { user: 'admin' });// 组件B(订阅事件)
import bus from './eventBus';
bus.on('user-login', (userData) => {console.log('用户登录:', userData);
});// 组件卸载时取消订阅
onUnmounted(() => {bus.off('user-login');
});
5. pinia状态管理(推荐)

优势:类型安全、Devtools支持、模块化设计
对比Vuex:更简洁API,去除mutations概念

// stores/user.js
import { defineStore } from 'pinia';
export const useUserStore = defineStore('user', {state: () => ({ name: '', isLogin: false }),actions: {login(name) {this.name = name;this.isLogin = true;}}
});// 组件中使用
<script setup>
import { useUserStore } from '@/stores/user';
const userStore = useUserStore();const login = () => {userStore.login('张三');
};
</script><template><div>用户名: {{ userStore.name }}</div>
</template>
6. 模板引用通信

适用场景:父组件需要直接访问子组件方法或数据
限制:只能在父子组件间使用

<!-- 父组件 -->
<script setup>
import Child from './Child.vue';
import { ref, onMounted } from 'vue';const childRef = ref(null);onMounted(() => {// 调用子组件方法childRef.value?.childMethod();// 访问子组件数据console.log(childRef.value?.childData);
});
</script><template><Child ref="childRef" />
</template><!-- 子组件 Child.vue -->
<script setup>
import { defineExpose } from 'vue';const childData = ref('子组件数据');
const childMethod = () => {console.log('子组件方法被调用');
};// 暴露给父组件
defineExpose({childData,childMethod
});
</script>
7. 作用域插槽(子→父通信)

适用场景:子组件需要向父组件传递渲染内容
优势:保持子组件封装性的同时提供定制能力

<!-- 子组件 ScopedList.vue -->
<script setup>
const items = ref(['Vue', 'React', 'Angular']);
</script><template><ul><li v-for="(item, index) in items" :key="index"><slot :item="item" :index="index" /></li></ul>
</template><!-- 父组件 -->
<script setup>
import ScopedList from './ScopedList.vue';
</script><template><ScopedList v-slot="{ item, index }"><span :class="{ active: index === 0 }">{{ item }}</span></ScopedList>
</template>
8. Web Workers 跨线程通信

适用场景:CPU密集型任务,避免阻塞UI线程
注意:worker中无法访问DOM

// worker.js
self.onmessage = (e) => {const result = heavyCalculation(e.data);self.postMessage(result);
};function heavyCalculation(data) {// 复杂计算逻辑return data * 2;
}// 组件中使用
<script setup>
import { ref } from 'vue';const worker = new Worker('./worker.js');
const result = ref(0);worker.onmessage = (e) => {result.value = e.data;
};const startCalc = () => {worker.postMessage(1000000); // 发送大数据
};
</script><template><button @click="startCalc">开始计算</button><div>结果: {{ result }}</div>
</template>

通信方式对比指南

通信方式

适用场景

优点

缺点

Props/Emits

父子组件简单通信

简单直接

多层传递繁琐

v-model

表单组件双向绑定

语法简洁

仅适用特定场景

Provide/Inject

跨层级组件共享

避免逐层传递

数据来源不透明

事件总线

任意组件间事件通知

灵活解耦

难以跟踪调试

Pinia/Vuex

全局状态管理

集中管理,调试友好

学习成本较高

模板引用

父组件访问子组件内部

精确访问

破坏组件封装性

作用域插槽

子组件向父组件暴露渲染数据

灵活定制UI

仅适用于模板内容

Web Workers

后台计算任务

避免UI阻塞

通信成本高

实战选型建议

  1. 父子组件:优先使用 props/emits + v-model

  2. 兄弟组件:采用共享父组件状态 或 事件总线

  3. 祖孙组件:使用 provide/inject

  4. 复杂应用Pinia 管理全局状态

  5. 性能敏感Web Workers 处理计算密集型任务

  6. UI定制作用域插槽 实现内容分发


文章转载自:

http://ernp6ZiG.gtkyr.cn
http://Xz4TtU1G.gtkyr.cn
http://m8V5OE9L.gtkyr.cn
http://SRvZX4yK.gtkyr.cn
http://ifM0SbQT.gtkyr.cn
http://p8cJYWvr.gtkyr.cn
http://34brw6W1.gtkyr.cn
http://nyxyVfKL.gtkyr.cn
http://UCQ6ZLot.gtkyr.cn
http://1GE9tkYY.gtkyr.cn
http://7QUGXXic.gtkyr.cn
http://5KQQdfjt.gtkyr.cn
http://Gp8gwcIe.gtkyr.cn
http://2TWF8O9c.gtkyr.cn
http://kznqP11c.gtkyr.cn
http://zpR49YY2.gtkyr.cn
http://YAEiBVQA.gtkyr.cn
http://PepfHUyM.gtkyr.cn
http://dkcp0w6e.gtkyr.cn
http://tD5e8xBE.gtkyr.cn
http://aps3qH5d.gtkyr.cn
http://WcA8u9Zq.gtkyr.cn
http://Tpppdd1m.gtkyr.cn
http://A242f9Ca.gtkyr.cn
http://BOfNDLv2.gtkyr.cn
http://MB8wuQWU.gtkyr.cn
http://nDwbp7Zy.gtkyr.cn
http://C5Fm7BIu.gtkyr.cn
http://Zoo9zitO.gtkyr.cn
http://0wZOcWOl.gtkyr.cn
http://www.dtcms.com/wzjs/753608.html

相关文章:

  • 票务网站模板广州开发区
  • a站是指哪个网站网站icp备案怎么查询
  • 学网站开发工程师难学吗企业精神标语
  • 360网站收录提交入口大全网页游戏知乎
  • 链接关系 网站层次结构哈尔滨工业大学包机
  • 陕西网站开发公司电话青海建筑网站建设公司
  • jsp网站开发中常见问题长春建站公司
  • 湖南旅游免费网站优化怎么做
  • 北京商城网站建设vps搭建wordpress
  • 前端做网站一般用什么框架用dw怎么做用户登录页面的网站
  • 用什么网站可以做电子书北京网站建设平台
  • 成都网站建设小公司排名wordpress 扫码插件
  • 做网站宝安中山网站开发费用
  • 建站工具有什么用好看网站推荐货源
  • wordpress网站搬家vps如何上传网站到云主机
  • 宁波网站设计哪家公司好深圳网站公司网站建设
  • 网站整合营销推广美术教育机构网站建设方案
  • 上海网站建设专业公司排名无锡网络公司无锡网站制作
  • 红酒网站源码代理网点
  • 网站空间就是虚拟主机吗网站设计的实例
  • 网站建设互联永康手工活外发加工网
  • 网站联盟营销哪个网站可以做图片
  • 快速建站视频任何人任意做网站销售产品违法吗
  • 公司seo是什么东莞seo排名公司
  • 锡山建设局网站wordpress怎么爆出版本
  • dedeseo网站响应式手机网站制作
  • 西安 餐饮 网站建设龙岗网站建设要多少钱
  • 建设收费网站网站色彩学
  • 怎么查公司网站可信度制作灯笼作文300字
  • 进入qq空间登录seo优化报价公司