Vue父子组件传值的一些坑

yizhihongxing

本文将为大家详细介绍Vue中父子组件传值的注意事项和实现方式。我们会从以下几个方面进行讲解:

1.父组件向子组件的传值
2.子组件向父组件的传值
3.中央事件总线(Event Bus)方式传值
4.Vuex状态管理方式传值

  1. 父组件向子组件的传值

父组件向子组件传值的方式有两种,一种是通过Props方式,一种是通过$children访问子组件的方式。

  • Props方式

父组件可以通过props方式将数据传递给子组件。子组件通过props的方式接收数据,进行处理和渲染。

<template>
  <div>
    <child-component :message="messageFromParent"></child-component>
  </div>
</template>

<script>
import ChildComponent from "./ChildComponent.vue";

export default {
  name: "ParentComponent",
  data() {
    return {
      messageFromParent: "Hello, child component!"
    };
  },
  components: {
    ChildComponent // 注册子组件
  }
};
</script>
<template>
  <div>
    <p>{{ message }}</p>
  </div>
</template>

<script>
export default {
  name: "ChildComponent",
  props: ["message"] // 定义Props
};
</script>
  • $children访问子组件的方式

另一种方式是通过$children访问子组件实例的方式传递数据。这种方式需要注意的是,使用$children需要等到子组件渲染完毕之后才能生效,否则会出现undefined的情况。

<template>
  <div>
    <button @click="changeValueInChild">子组件中的值加一</button>
    <child-component ref="child"></child-component>
  </div>
</template>

<script>
import ChildComponent from "./ChildComponent.vue";

export default {
  name: "ParentComponent",
  methods: {
    changeValueInChild() {
      this.$refs.child.value++;
    }
  },
  components: {
    ChildComponent
  }
};
</script>
<template>
  <div>
    <p>子组件中的值:{{ value }}</p>
  </div>
</template>

<script>
export default {
  name: "ChildComponent",
  data() {
    return {
      value: 0
    };
  }
};
</script>
  1. 子组件向父组件的传值

子组件向父组件传值的方式有两种,一种是通过emit方式发送事件,一种是通过\$parent访问父组件实例的方式进行传递数据。

  • emit方式

子组件通过Vue中的emit方法,向父组件发送自定义事件,并且可以携带数据。父组件监听该事件,获取子组件的数据。

<template>
  <div>
    <button @click="sendToParent">将子组件中的值传递给父组件</button>
  </div>
</template>

<script>
export default {
  name: "ChildComponent",
  data() {
    return {
      value: 0
    };
  },
  methods: {
    sendToParent() {
      this.$emit("increment", this.value);
    }
  }
};
</script>
<template>
  <div>
    <p>父组件中的值:{{ parentValue }}</p>
    <child-component @increment="getValueFromChild"></child-component>
  </div>
</template>

<script>
import ChildComponent from "./ChildComponent.vue";

export default {
  name: "ParentComponent",
  data() {
    return {
      parentValue: 0
    };
  },
  methods: {
    getValueFromChild(value) {
      this.parentValue += value;
    }
  },
  components: {
    ChildComponent
  }
};
</script>
  • \$parent访问父组件实例的方式

另一种方式是通过\$parent访问父组件实例的方式进行传递数据。可以直接修改父组件的数据,从而达到传值的目的。这种方式需要注意的是,访问父组件实例时需要等到父组件渲染完毕之后才能生效,否则会出现undefined的情况。

<template>
  <div>
    <button @click="sendToParent">将子组件中的值传递给父组件</button>
  </div>
</template>

<script>
export default {
  name: "ChildComponent",
  data() {
    return {
      value: 0
    };
  },
  methods: {
    sendToParent() {
      this.$parent.parentValue += this.value;
    }
  }
};
</script>
<template>
  <div>
    <p>父组件中的值:{{ parentValue }}</p>
    <child-component></child-component>
  </div>
</template>

<script>
import ChildComponent from "./ChildComponent.vue";

export default {
  name: "ParentComponent",
  data() {
    return {
      parentValue: 0
    };
  },
  components: {
    ChildComponent
  }
};
</script>
  1. 中央事件总线(Event Bus)方式传值

中央事件总线是一种Vue中比较常用的数据传递方式。通过创建Vue实例作为中央事件总线,实现父组件和子组件之间的通信。子组件通过总线\$emit自定义事件,并通过总线\$on方法监听事件,父组件通过总线实例去监听事件,从而获得子组件传过来的值。

<template>
  <div>
    <button @click="sendEvent">将子组件中的值传递给父组件</button>
  </div>
</template>

<script>
export default {
  name: "ChildComponent",
  data() {
    return {
      value: 0
    };
  },
  methods: {
    sendEvent() {
      this.$bus.$emit("increment", this.value);
    }
  }
};
</script>
<template>
  <div>
    <p>父组件中的值:{{ parentValue }}</p>
  </div>
</template>

<script>
export default {
  name: "ParentComponent",
  data() {
    return {
      parentValue: 0
    };
  },
  mounted() {
    this.$bus.$on("increment", value => {
      this.parentValue += value;
    });
  }
};
</script>

在使用中央事件总线的方式传值时需要注意的是,如果不注意及时移除监听事件,在组件销毁后还会接收到子组件的数据。因此,在组件销毁前一定要通过\$off方法将事件监听移除。

  1. Vuex状态管理方式传值

Vuex是Vue中官方推荐使用的状态管理器,因此在使用Vue的过程中有些情况下也可以通过Vuex来实现组件之间通信。

Vuex中的mutation和action可以改变state中的数据,因此可以将需要传递的值存储在state中,从而实现父组件和子组件之间的通信。父组件通过dispatch调用action,从而触发mutation来改变state中的值,子组件通过$store来获取需要的值。

<template>
  <div>
    <button @click="sendToParent">将子组件中的值传递给父组件</button>
  </div>
</template>

<script>
export default {
  name: "ChildComponent",
  data() {
    return {
      value: 0
    };
  },
  methods: {
    sendToParent() {
      this.$store.dispatch("changeParentValue", this.value);
    }
  }
};
</script>
<template>
  <div>
    <p>父组件中的值:{{ parentValue }}</p>
    <child-component></child-component>
  </div>
</template>

<script>
import ChildComponent from "./ChildComponent.vue";

export default {
  name: "ParentComponent",
  computed: {
    parentValue() {
      return this.$store.state.parentValue;
    }
  },
  methods: {
    changeParentValue(value) {
      this.$store.commit("changeParentValue", value);
    }
  },
  components: {
    ChildComponent
  }
};
</script>
// store.js

import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

const state = {
  parentValue: 0
};

const mutations = {
  changeParentValue(state, value) {
    state.parentValue += value;
  }
};

const actions = {
  changeParentValue({ commit }, value) {
    commit("changeParentValue", value);
  }
};

export default new Vuex.Store({
  state,
  mutations,
  actions
});

总结:

以上就是Vue中父子组件传值的一些注意事项和实现方式,通过对这些方式的了解和掌握,我们可以更好地实现组件之间的通信,提高Vue的开发效率。同时,在使用不同的传值方式时也需要根据具体情况进行权衡选择,以达到最佳的开发体验。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Vue父子组件传值的一些坑 - Python技术站

(0)
上一篇 2023年5月28日
下一篇 2023年5月28日

相关文章

  • Spring Boot 中starter的原理详析

    关于“Spring Boot 中starter的原理详析”,我会给出以下完整攻略: 一、什么是 Starter Spring Boot 中的起步依赖 Starter 是一个 Maven 项目,是一种便于其他 Spring Boot 项目使用的“快捷方式”。Starter 可以包括许多常用的库和依赖,例如 Spring Boot Web Starter,它包括…

    Vue 2023年5月28日
    00
  • Vue中请求本地JSON文件并返回数据的方法实例

    可以采用 Vue-Resource 插件来请求本地JSON文件,并解析返回的数据。 第一步,需要在项目中引入 Vue-Resource 插件。可以通过以下命令进行安装: npm install vue-resource –save 然后在 main.js 文件中引入 Vue-Resource 并使用: import Vue from ‘vue’; impo…

    Vue 2023年5月28日
    00
  • Vue打包后访问静态资源路径问题

    Vue是一种流行的JavaScript框架,用于构建交互式的现代Web应用程序。Vue的打包可以将应用程序的所有资源(例如HTML,JavaScript,CSS和图像)打包成几个文件,使应用程序易于部署。但是,Vue打包后,用户在访问应用程序时可能会遇到静态资源路径问题。为解决这一问题,我们可以采用以下攻略: 问题描述 Vue打包后,由于路径设置不正确,可能…

    Vue 2023年5月28日
    00
  • 详解如何在vue-cli中使用vuex

    下面为您详细讲解如何在vue-cli中使用vuex。 什么是Vuex? Vuex是一个专为Vue.js应用程序开发的状态管理模式和库。它采用集中式存储管理应用的所有组件的状态,并以可预测的方式发起状态的改变。它的核心概念包括Store、State、Getter、Mutation、Action和Module。 如何在vue-cli中使用vuex? 以下是一些简…

    Vue 2023年5月27日
    00
  • vue 2.0项目中如何引入element-ui详解

    当我们想利用 Element UI 搭建 Vue 2.0 项目时,首先需要引入 Element UI。下面就是详细的操作步骤: 一、安装 Element UI Element UI 是一个基于 Vue.js 的桌面端组件库,提供了众多酷炫的组件,且使用方便。我们可以通过 npm 来进行安装 Element UI。 在命令行中执行以下命令: npm i ele…

    Vue 2023年5月28日
    00
  • 在vue自定义组件中使用 v-model指令详情

    下面是详细讲解“在vue自定义组件中使用 v-model指令”的攻略。 什么是v-model v-model 指令在表单元素上创建了双向数据绑定。当表单元素的值发生变化时,其绑定的数据也会跟着变化;反之,当数据发生变化时,表单元素的值也会跟着变化。v-model在vue中非常常用。 在Vue自定义组件中使用v-model 在 Vue 自定义组件中使用 v-m…

    Vue 2023年5月28日
    00
  • Vue实例初始化为渲染函数设置检查源码剖析

    首先,了解Vue实例的初始化过程非常重要。在创建Vue实例时,Vue会进行一些默认的初始化步骤,其中之一就是为渲染函数设置一些检查源码,以便在开发过程中发现错误。 具体来说,Vue会在初始化过程中调用initRender函数,该函数的主要作用是为渲染函数设置检查源码。其中,Vue会创建一个 Watcher对象并将其添加到当前Vue实例的watchers数组中…

    Vue 2023年5月28日
    00
  • 详解vue2.0脚手架的webpack 配置文件分析

    下面我将为你详细讲解“详解vue2.0脚手架的webpack 配置文件分析”的完整攻略。 一、前言 在进行Vue2.0开发时,使用脚手架可以减少很多配置时间,提高开发效率。而其中的webpack配置文件对整个应用的打包和构建起到了重要的作用。因此,对webpack配置文件进行分析和解读是非常必要的。 二、webpack 配置文件分析 2.1 入口文件配置 在…

    Vue 2023年5月28日
    00
合作推广
合作推广
分享本页
返回顶部