vue基础(八)

devtools/2025/2/13 19:56:22/

在 Vue 中,组件之间的传值方式主要包括以下几种情况:

1. 父组件向子组件传值(props

父组件通过 props 传递数据给子组件:

<!-- Parent.vue -->
<template><ChildComponent :msg="message" />
</template><script>
import ChildComponent from './ChildComponent.vue';
export default {components: { ChildComponent },data() {return {message: 'Hello from Parent!'};}
};
</script>
<!-- ChildComponent.vue -->
<template><p>Received message: {{ msg }}</p>
</template><script>
export default {props: {msg: String}
};
</script>

2. 子组件向父组件传值($emit

子组件通过 this.$emit 触发事件,父组件监听事件并获取值:

<!-- Parent.vue -->
<template><ChildComponent @update-message="handleMessage" /><p>Message from Child: {{ receivedMessage }}</p>
</template><script>
import ChildComponent from './ChildComponent.vue';
export default {components: { ChildComponent },data() {return {receivedMessage: ''};},methods: {handleMessage(msg) {this.receivedMessage = msg;}}
};
</script>
<!-- ChildComponent.vue -->
<template><button @click="sendMessage">Send to Parent</button>
</template><script>
export default {methods: {sendMessage() {this.$emit('update-message', 'Hello from Child!');}}
};
</script>

3. 兄弟组件传值(Event BusPinia/Vuex

兄弟组件需要一个中间桥梁,比如 Event Bus(Vue 3 不推荐)或 Pinia(推荐):

// eventBus.js (Vue 2 可用,Vue 3 推荐使用 Pinia)
import Vue from 'vue';
export const EventBus = new Vue();

BrotherA.vue 发送数据:

<template><button @click="sendMessage">Send to BrotherB</button>
</template><script>
import { EventBus } from './eventBus.js';
export default {methods: {sendMessage() {EventBus.$emit('message', 'Hello from BrotherA!');}}
};
</script>

BrotherB.vue 接收数据:

<template><p>{{ receivedMessage }}</p>
</template><script>
import { EventBus } from './eventBus.js';
export default {data() {return { receivedMessage: '' };},created() {EventBus.$on('message', msg => {this.receivedMessage = msg;});}
};
</script>

4. ref 方式(获取子组件实例)

父组件可以通过 ref 获取子组件实例并访问其方法或数据:

<!-- Parent.vue -->
<template><ChildComponent ref="childRef" /><button @click="callChildMethod">Call Child Method</button>
</template><script>
import ChildComponent from './ChildComponent.vue';
export default {components: { ChildComponent },methods: {callChildMethod() {this.$refs.childRef.childMethod();}}
};
</script>
<!-- ChildComponent.vue -->
<template><p>Child Component</p>
</template><script>
export default {methods: {childMethod() {console.log('Child method called!');}}
};
</script>

5. provideinject(适用于祖孙组件)

适用于跨层级组件通信:

<!-- GrandParent.vue -->
<template><Parent />
</template><script>
import Parent from './Parent.vue';
export default {components: { Parent },provide() {return { sharedMessage: 'Hello from GrandParent!' };}
};
</script>
<!-- Parent.vue -->
<template><Child />
</template><script>
import Child from './Child.vue';
export default {components: { Child }
};
</script>
<!-- Child.vue -->
<template><p>{{ sharedMessage }}</p>
</template><script>
export default {inject: ['sharedMessage']
};
</script>

6. Vuex 或 Pinia(全局状态管理)

适用于复杂状态管理,如 Vuex(Vue 2)或 Pinia(Vue 3):

// store.js (使用 Pinia)
import { defineStore } from 'pinia';export const useMainStore = defineStore('main', {state: () => ({message: 'Hello from Store'}),actions: {setMessage(newMsg) {this.message = newMsg;}}
});

ComponentA.vue 更新数据:

<template><button @click="updateMessage">Update Message</button>
</template><script>
import { useMainStore } from './store.js';
export default {setup() {const store = useMainStore();const updateMessage = () => store.setMessage('Updated Message!');return { updateMessage };}
};
</script>

ComponentB.vue 读取数据:

<template><p>{{ store.message }}</p>
</template><script>
import { useMainStore } from './store.js';
export default {setup() {const store = useMainStore();return { store };}
};
</script>


http://www.ppmy.cn/devtools/158572.html

相关文章

arcgis for js实现平移立体效果

在web&#xff08;GIS&#xff09;开发中&#xff0c;利用 ArcGIS API for JavaScript 实现各种炫酷的地图效果是很常见的需求。本文将介绍如何使用 ArcGIS API for JavaScript 实现平移立体效果&#xff0c;通过加载边界线&#xff0c;根据边界线平移生成新的面&#xff0c;再…

Spring MVC 拦截器(Interceptor)与过滤器(Filter)的区别?

1、两者概述 拦截器&#xff08;Interceptor&#xff09;&#xff1a; 只会拦截那些被 Controller 或 RestController 标注的类中的方法处理的请求&#xff0c;也就是那些由 Spring MVC 调度的请求。过滤器&#xff08;Filter&#xff09;&#xff1a; 会拦截所有类型的 HTTP …

redis复制

文章目录 复制功能的实现 部分冲同步实现复制偏移量复制积压缓冲区复制积压缓冲区的大小能否调整&#xff1f;&#xff1f;&#xff1f; 服务器运行ID PSYNC命令的实现复制的实现心跳检测检测主从服务器的网络连接状态辅助实现&#xff01;min-slaves配置选项检测命令去失 总结…

AF3 superimpose函数解读

AlphaFold3 superimpose函数通过使用SVD最小化RMSD&#xff0c;将坐标叠加到参考上&#xff0c;在蛋白质结构预测中用于比较预测结构与真实结构的相似性。 源代码&#xff1a; from src.utils.geometry.alignment import weighted_rigid_align from src.utils.geometry.vect…

React 第二十五节 <Fragment></Fragment> 的用途以及使用注意事项详解

文章如果错误偏差&#xff0c;烦请及时批评指正 一、为什么要使用 <Fragment>&#xff1f; 因为在 React 中&#xff0c;组件必须返回单个根元素。当我们尝试直接返回相邻的 JSX 元素时&#xff1a; function BrokenComponent() {return (<h1>标题</h1><…

在 Mac ARM 架构上使用 nvm 安装 Node.js 版本 16.20.2

文章目录 1. 安装 nvm&#xff08;如果还没有安装的话&#xff09;2. 加载 nvm 配置3. 列出特定系列的 Node.js 版本&#xff08;远程&#xff09;&#xff1a;4. 安装 Node.js 16.20.25. 使用指定版本的 Node.js6. 验证安装 在 Mac ARM 架构上使用 nvm 安装 Node.js 版本 16.…

anolis os 8.9-jenkins2.492.1-role-base

一、插件安装 二、新建用户 三、manage roles 3.1 manage role 配置 3.2 Assign Roles

以什么方式维护html网页的多语言版本比较好

维护 HTML 网页的多语言版本有几种常见且有效的方式&#xff0c;主要取决于你的需求和项目规模。以下是几种常用的方式&#xff1a; 不同的 HTML 文件&#xff08;按语言分文件&#xff09;&#xff1a; 优点&#xff1a;每种语言都有一个独立的 HTML 文件&#xff0c;容易管理…