Vue中的子传父、父传子详细(vue2子传父、父传子,vue3子传父、父传子)

落日映苍穹つ 2024-04-22 22:30 132阅读 0赞

简介:

  • 在Vue中,组件之间的数据传递是一个非常重要的概念。父子组件之间的数据传递,特别是子组件向父组件传递数据(子传父),是构建复杂Vue应用的基础。

  • 子传父,在父组件中给子组件标签绑定一个自定义事件,给这个事件挂载需要调用的方法,然后在子组件的方法通过this.$emit(‘自定义事件名’)来调用这个方法。

  • 父传子,在父组件中给子组件标签绑定一个自定义事件,给这个事件挂载需要调用的方法,在子组件的方法通过this.$emit(‘自定义事件名’)来调用这个方法。

⭐Vue2中的子传父、父传子

子传父(Vue2)

一、基本原理

在Vue中,子组件向父组件传递数据通常是通过触发自定义事件来实现的。子组件中可以通过$emit方法触发一个事件,并传递一些数据给父组件,而父组件则可以通过监听这个事件来接收子组件传递的数据。

二、代码实例

子组件的实现 1、首先,我们创建一个子组件ChildComponent,并在其中定义一个方法用于触发事件传递数据给父组件。

  1. <!-- ChildComponent.vue -->
  2. <template>
  3. <div>
  4. <button @click="choseSend">发送给父组件</button>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. data() {
  10. return {
  11. msg: '这是子组件的消息'
  12. };
  13. },
  14. methods: {
  15. choseSend() {
  16. // 通过$emit触发一个名为'child-message'的自定义事件,并传递message数据
  17. this.$emit('childMessage', this.msg);
  18. }
  19. }
  20. };
  21. </script>

2、父组件的实现 接下来,我们在父组件ParentComponent中使用ChildComponent,并监听child-message事件来接收子组件传递的数据。

  1. <!-- ParentComponent.vue -->
  2. <template>
  3. <div>
  4. <h2>父组件</h2>
  5. <p>从子组件接收的消息: {
  6. { receivedMessage }}</p>
  7. <ChildComponent @childMessage="handleChildMessage" />
  8. </div>
  9. </template>
  10. <script>
  11. import ChildComponent from './ChildComponent.vue';
  12. export default {
  13. components: {
  14. ChildComponent,
  15. //可以使用组件懒加载
  16. //ChildComponent: () => import("./ChildComponent.vue"),
  17. },
  18. data() {
  19. return {
  20. receivedMsg: ''
  21. };
  22. },
  23. methods: {
  24. handleChildMessage(msg) {
  25. // 父组件接收到子组件通过事件传递的数据,并更新本地数据
  26. this.receivedMsg = msg;
  27. }
  28. }
  29. };
  30. </script>

以上就是子传父的具体过程,在上面代码中,我们做了以下操作:

  • 在ChildComponent中,我们定义了一个choseSend方法,当按钮被点击时,通过this.$emit触发一个名为childMessage的事件,并传递msg数据。
  • 在ParentComponent中,我们引入了ChildComponent,并在模板中使用它。同时,我们监听了childMessage事件,并在事件触发时调用handleChildMessage方法,将接收到的数据存储在receivedMsg中。

三、本章小结,子传父是Vue组件间通信的一种常见方式,通过自定义事件,子组件可以灵活地传递数据给父组件。在实际开发中,我们需要根据具体需求设计事件名称和数据结构,确保组件之间的通信清晰、高效。

父传子(Vue2)

一、基本步骤

在父组件中定义要传递给子组件的数据,需要在父组件的模板中,使用子组件标签,并通过属性(props)将数据传递给子组件。在子组件中,通过props选项来声明接收的数据。

二、代码示例

1、父组件(ParentComponent.vue)

  1. <template>
  2. <div>
  3. <h2>父组件</h2>
  4. <!-- 使用子组件,并通过属性将数据传递给子组件 -->
  5. <ChildComponent :message="parentMessage" />
  6. </div>
  7. </template>
  8. <script>
  9. import ChildComponent from './ChildComponent.vue';
  10. export default {
  11. components: {
  12. ChildComponent // 注册子组件
  13. },
  14. data() {
  15. return {
  16. // 定义要传递给子组件的数据
  17. parentMessage: '这是父组件传递给子组件的消息'
  18. };
  19. }
  20. };
  21. </script>

在父组件中,我们定义了一个parentMessage数据,然后在模板中使用将parentMessage作为属性传递给子组件。这里属性名前面的冒号 :message 是Vue的特殊语法,表示这是一个动态绑定,即绑定的值是响应式的,如果父组件中parentMessage的值发生改变,子组件接收到的值也会相应地更新。

2. 子组件(ChildComponent.vue)

  1. <template>
  2. <div>
  3. <h3>子组件</h3>
  4. <p>{
  5. { message }}</p> <!-- 在模板中直接使用props中的数据 -->
  6. </div>
  7. </template>
  8. <script>
  9. export default {
  10. props: {
  11. // 声明接收的数据
  12. message: {
  13. type: String, // 指定数据的类型
  14. required: true, // 指定该数据是必需的
  15. default: '' // 指定默认值
  16. }
  17. },
  18. //或者使用props的数组形式接收数据,这样不用指定类型,二选一
  19. props: ["message"],
  20. };
  21. </script>

在子组件中,我们通过props选项声明了一个message属性,用来接收父组件传递过来的数据。在模板中,我们可以直接使用{ { message }}来显示这个数据。

三、注意事项

  1. props的命名:在子组件中声明props时,尽量使用camelCase(驼峰命名法),而在父组件中传递props时,可以使用kebab-case(短横线分隔命名法),Vue会自动将短横线命名转换为驼峰命名。

  2. props的类型和验证:在子组件中声明props时,可以指定其类型、是否必需以及默认值,这样有助于确保父组件传递过来的数据符合预期。

  3. 避免直接修改props:在子组件中,应该避免直接修改通过props传入的数据,因为props是单向数据流,子组件不应该修改父组件的状态。如果需要基于props进行某些操作或计算,可以在子组件中定义计算属性或方法。

⭐Vue3中的子传父、父传子(一、二)

子传父(Vue3)

一、实例代码,使用语法糖

1、子组件,ChildComponent.vue

  1. <template>
  2. <button @click="sendToParent">发送消息给父组件</button>
  3. </template>
  4. <script setup>
  5. import { defineEmits } from 'vue';
  6. // 定义组件可以触发的自定义事件
  7. const emit = defineEmits(['child-message']);
  8. // 定义要发送给父组件的消息
  9. const message = '这是子组件的消息';
  10. // 定义发送消息给父组件的方法
  11. const sendToParent = () => {
  12. // 使用 emit 触发自定义事件并传递数据
  13. emit('child-message', message);
  14. };
  15. </script>

ChildComponent.vue 组件中的注释解释了每一部分代码的作用。我们定义了一个 message 常量来存储要发送给父组件的消息,然后创建了一个 sendToParent 方法来触发 child-message 事件并传递消息。

2、父组件,ParentComponent.vue

  1. <template>
  2. <div>
  3. <h2>父组件</h2>
  4. <p>从子组件接收的消息: {
  5. { receivedMessage }}</p>
  6. <!-- 在父组件模板中使用子组件,并监听 child-message 事件 -->
  7. <ChildComponent @child-message="handleChildMessage" />
  8. </div>
  9. </template>
  10. <script setup>
  11. import { ref } from 'vue';
  12. import ChildComponent from './ChildComponent.vue';
  13. // 创建一个响应式数据来存储从子组件接收到的消息
  14. const receivedMessage = ref('');
  15. // 定义处理子组件发送消息的方法
  16. const handleChildMessage = (message) => {
  17. // 更新父组件中接收到的消息
  18. receivedMessage.value = message;
  19. };
  20. </script>

ParentComponent.vue 组件中的注释解释了如何在模板中使用子组件并监听 child-message 事件。在 语法糖" class="reference-link">二、实例代码,使用语法糖

1、父组件,ParentComponent.vue

  1. <template>
  2. <div>
  3. <h2>父组件</h2>
  4. <!-- 在父组件中使用子组件,并通过 props 传递数据 -->
  5. <ChildComponent :message="parentMessage" />
  6. </div>
  7. </template>
  8. <script setup>
  9. import { ref } from 'vue';
  10. import ChildComponent from './ChildComponent.vue';
  11. // 声明子组件
  12. defineComponents({
  13. ChildComponent
  14. });
  15. // 定义响应式数据 parentMessage
  16. const parentMessage = ref('这是父组件传递给子组件的消息');
  17. </script>

在这个中组件中导入了 ref 函数来创建一个响应式数据 parentMessage,并通过 props 将这个数据传递给子组件 ChildComponent,并且在父组件的模板中,通过 :message=”parentMessage” 将数据绑定到子组件的 message prop 上。

2、子组件,ChildComponent.vue

  1. <template>
  2. <div>
  3. <h3>子组件</h3>
  4. <!-- 显示从父组件接收到的 message -->
  5. <p>{
  6. { message }}</p>
  7. </div>
  8. </template>
  9. <script setup>
  10. import { defineProps } from 'vue';
  11. // 使用 defineProps 声明从父组件接收的 props
  12. const props = defineProps({
  13. message: {
  14. type: String, // 指定 message 的类型为字符串
  15. required: true, // message 是必需的
  16. default: '' // 默认值
  17. },
  18. });
  19. //同样,这里可以使用数组形式声明 props ,这样不用指定类型,二选一
  20. const props = defineProps(['message']);
  21. </script>

子组件使用 defineProps 函数来声明从父组件接收的 message prop。在子组件的模板中,通过插值表达式 { { message }} 来显示接收到的消息。

二、小结

发表评论

表情:
评论列表 (有 0 条评论,132人围观)

还没有评论,来说两句吧...

相关阅读