Vue 父子组件传值

 项目中往往会把一些常用的公共代码抽离出来,写成一个子组件。或者在一个页面中的代码太多,可以根据功能的不同抽离出相关代码写成子组件,这样代码结构会更加简洁明了,后续维护更加方便。应用组件的时候就会涉及到组件之间相互传递参数以下进行简单的介绍,vue中的父子组件传值,要注意的是遵守单向数据流原则。所谓单向数据流原则,简单的说就是父组件的数据可以传递给子组件,子组件也可以正常获取并使用由父组件传过来的数据;但是,子组件中不能直接修改父组件传过来的数据,必须要向父组件传递一个事件来父组件需要修改数据,即通过子组件的操作,在父组件中修改数据;这就是单项数据流。

目录

Vue父子传值的方法

1.props / $emit

父传子

子传父

2.$ref

在父组件中定义ref

在父组件中取值:

3.$parent / children

 children 在父组件直接使用进行打印

 parent 在子组件进行调用打印


Vue父子传值的方法

在Vue项目进行父子传值主要有以下三种方法:

1. props / $emit                

2. $ref                

3. $parent / children

1.props / $emit

⼦组件中通过定义props接收⽗组件中通过v-bind绑定的数据

父传子

首先在父组件中定义一个data数据

import ChildGroup from "./ChildGroup.vue";// 引入组件
export default {
  components: {
    ChildGroup,// 注册组件
  },
  data() {
    return {
      Text: 111111111111,// 需要传递的值
      Text2: 22222222222,// 需要传递的值
    };
  },
};

在父组件中使用子组件时用 v-bind 定义一个属性并将父组件的 Text Text2 传入

<ChildGroup :Text="Text" :Text2="Text2"/>

在子组件中使用props接收传入的属性,并可以直接使用

<template>
  <div>
    <h2>子组件</h2>
    <div>传递的值:</div>
    <div>{{ Text }}</div>
    <div>{{ Text2 }}</div>
  </div>
</template>

<script>
export default {
  props: {
    Text: {// 接收的名称
      type: Object, //接受类型
      default: {}, //默认值
    },
    Text2: {
      type: String,
      default: {},
    },
  },
};
</script>

父组件传入子组件的值就被显示出来了:

子传父

首先在子组件中定义一个事件,并且使用emit发送给父组件,在子组件使用的click事件触发发送自定义事件

<template>
  <div>
    <h2>子组件</h2>
    <button @click="childadd">子传父</button>
  </div>
</template>
<script>
export default {
  data() {
    return {
      list: 666666666,
    };
  },
  methods: {
    childadd() {
        // this.$emit(‘自定义事件名’,所需要传递的值)
      this.$emit("childadd", this.list);
    },
  },
};
</script>

在父组件中需要定义方法 接受自定义事件 :

<template>
  <div class="home">
    <h1>父组件</h1>
    ---------------------------------------------------------------
    <ChildGroup @childadd="childadd" />
  </div>
</template>

<script>
import ChildGroup from "./ChildGroup.vue";
export default {
  name: "AsdHomeView",
  components: {
    ChildGroup,
  },
  methods: {
    childadd(val) {
      console.log(val);
    },
  },
};
</script>

2.$ref

ref可以让父组件更加便利地取到想要的子组件,取到组件对象 可以使用值或者方法

在父组件中定义ref

<ChildGroup ref="childs" />

在父组件中取值:

console.log(this.$refs.childs); // 打印组件对象组件对象
console.log(this.$refs.childs.list);  // 直接调用子组件中相应的值

3.$parent / children

 children 在父组件直接使用进行打印

console.log(this.$children[0]);// 打印组件对象组件对象
console.log(this.$children[0].list); // 直接调用子组件中相应的值

 parent 在子组件进行调用打印

console.log(this.$parent); // 打印组件对象组件对象
console.log(this.$parent.Text); // 直接调用父组件中相应的值

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue是一种流行的JavaScript框架,用于构建用户界面。在Vue中,父子组件之间传递数据是一种常见的需求。以下是一种常用的方法来实现父子组件之间的数据传递: 1. Props(属性):父组件可以通过props属性向子组件传递数据。在父组件中,通过在子组件标签上绑定属性的方式传递数据。在子组件中,可以通过props选项接收并使用这些数据。 父组件: ```html <template> <div> <child-component :message="parentMessage"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, data() { return { parentMessage: 'Hello from parent component' }; } } </script> ``` 子组件: ```html <template> <div> <p>{{ message }}</p> </div> </template> <script> export default { props: ['message'] } </script> ``` 2. $emit(自定义事件):子组件可以通过$emit方法触发自定义事件,并将需要传递的数据作为参数传递给父组件。在父组件中,通过在子组件标签上监听自定义事件的方式接收数据。 父组件: ```html <template> <div> <child-component @child-event="handleChildEvent"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, methods: { handleChildEvent(data) { console.log(data); // 在这里处理子组件传递的数据 } } } </script> ``` 子组件: ```html <template> <div> <button @click="emitEvent">触发事件</button> </div> </template> <script> export default { methods: { emitEvent() { this.$emit('child-event', 'Hello from child component'); // 触发自定义事件,并传递数据给父组件 } } } </script> ``` 以上是Vue中实现父子组件之间传递数据的两种常用方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值