vue父子组件进行通信方式

x33g5p2x  于2022-02-12 转载在 Vue.js  
字(3.9k)|赞(0)|评价(0)|浏览(530)

在vue中如何实现父子组件通信,本篇博客将会详细介绍父子组件通信的流程。

如图所示,父组件向子组件传递数据,可以通过props,子组件向父组件传递数据可以通过触发事件来进行。
一、props
父组件向子组件传递的数据,通过props进行传递,我们可以把props理解为属性。props传递存在两种格式,一种是数组格式,另一种是对象类型格式。其中第二种对象类型可以设置是否为必须数据,以及是否存在默认值数据。
第一种用法:数组

  1. //父组件
  2. <HelloWorld :title="title"></HelloWorld>
  3. //子组件
  4. props: ["title"],

第二种用法:对象

  1. //父组件:
  2. <HelloWorld :title="title"></HelloWorld>
  3. //子组件:
  4. props: {
  5. title:{
  6. type:String,
  7. required:true,
  8. default() {
  9. return "我是title"
  10. }
  11. }
  12. },
  13. //上面default为什么是一个函数?
  14. 因为是一个组件,组件在其他组件都能使用,并且如果default是一个key;value形式,并且value是一个引用
  15. 类型的值,则如果要更改props的值,则其他组件的值也会更改。

type属性的类型有哪些?

  1. type属性的类型有:String,Number,Boolean,Array,Object,Date, Function,Symbol

三、对象类型的其他写法

  1. props:{
  2. messageinfo:String,
  3. propsA:Number
  4. propsC:{
  5. type:String,
  6. required:true
  7. },
  8. propsE:{
  9. type:Object,
  10. default(){
  11. return {message:"hello"}
  12. }
  13. },
  14. //自定义验证函数
  15. title:{
  16. validator(value) {
  17. console.log("hhh")
  18. return ["hello","world"].includes(value)
  19. }
  20. }
  21. }

二、细节三props大小写命名
props名使用驼峰命名,则可以使用-连接

  1. //父组件
  2. <HelloWorld :mess-age="title"></HelloWorld>
  3. //子组件
  4. props: {
  5. messAge:{
  6. type:String,
  7. }
  8. },

三、非props的attributes属性
如果在父组件中设置attributes,但是在子组件中的props不存在该属性,则如果子组件存在根节点,则就会该属性就会继承到根节点上。

如果我们不希望根节点继承,可以使用inhertAttrs:false,这样就可以继承到非根节点上。

  1. <template>
  2. <div>{{ messAge }}
  3. <h1 :class="$attrs.class">hhhhh</h1>
  4. </div>
  5. </template>

如果要是存在多个根节点,则就会显示warning,表示不能自动继承,此时我们可以使用$attrs.属性名来实现继承属性。

  1. <template>
  2. <h1>{{ messAge }}</h1>
  3. <h1>哈哈哈</h1>
  4. <h1 :class="$attrs.class">呵呵呵</h1>
  5. </template>

四、子组件传递给父组件

  1. 1、当子组件有一些事情发生的时候,比如在组件中发生点击,父组件需要切换内容。2
  2. 2、子组件有一些内容想要传递给父组件。
  3. 3、子组件通过$emit()触发事件,并且在emits中进行注册事件。
  4. 4、注册的事件可以是数组类型的,也可以是对象类型。

五、简单例子
数组格式

  1. //子组件
  2. <template>
  3. <button @click="increment">+1</button>
  4. <button @click="decrement">-1</button>
  5. </template>
  6. <script>
  7. export default {
  8. emits:["add", "sub"],
  9. data() {
  10. return {
  11. }
  12. },
  13. methods: {
  14. increment: function () {
  15. this.$emit("add")
  16. },
  17. decrement: function () {
  18. this.$emit("sub")
  19. },
  20. },
  21. };
  22. </script>
  23. <style scoped></style>
  1. //父组件
  2. <template>
  3. <h1>当前的数字是:{{counter}}</h1>
  4. <HelloWorld @add="addOne" @sub="subOne"></HelloWorld>
  5. </template>
  6. <script>
  7. import HelloWorld from "./components/HelloWorld.vue"
  8. export default {
  9. components: { HelloWorld },
  10. data() {
  11. return {
  12. counter: 0
  13. }
  14. },
  15. methods:{
  16. addOne() {
  17. this.counter++
  18. },
  19. subOne() {
  20. this.counter--
  21. }
  22. }
  23. }
  24. </script>
  25. <style scoped></style>

数组格式:如果我们想要设置自定义事件,可以使用emits:["add", "sub"],数组格式。
对象格式:主要是针对需要向父组件传递参数的例子.

  1. //父组件
  2. <template>
  3. <h1>当前的数字是:{{counter}}</h1>
  4. <HelloWorld @add="addOne" @sub="subOne" @addN="addNumbers"></HelloWorld>
  5. </template>
  6. <script>
  7. import HelloWorld from "./components/HelloWorld.vue"
  8. export default {
  9. components: { HelloWorld },
  10. data() {
  11. return {
  12. counter: 0,
  13. }
  14. },
  15. methods:{
  16. addOne() {
  17. this.counter++
  18. },
  19. subOne() {
  20. this.counter--
  21. },
  22. addNumbers(value) {
  23. this.counter += parseInt(value)
  24. }
  25. }
  26. }
  27. </script>
  28. <style scoped></style>
  1. //子组件
  2. <template>
  3. <button @click="increment">+1</button>
  4. <button @click="decrement">-1</button>
  5. <input type="text" v-model="num" />
  6. <button @click="incrementN">+N</button>
  7. </template>
  8. <script>
  9. export default {
  10. emits: {
  11. add:null,
  12. sub:null,
  13. addN:(dispatch) => {
  14. if(dispatch > 10) {
  15. return true
  16. }
  17. return false
  18. }
  19. },
  20. data() {
  21. return {
  22. num: 0,
  23. };
  24. },
  25. methods: {
  26. increment: function () {
  27. this.$emit("add");
  28. },
  29. decrement: function () {
  30. this.$emit("sub");
  31. },
  32. incrementN: function () {
  33. this.$emit("addN", this.num);
  34. },
  35. },
  36. };
  37. </script>
  38. <style scoped></style>

这里采用对象的格式:可以进行传入参数的判断。如果符合则返回true,如果不符合则返回false,但是仍可以执行,只是在控制台出现warning.

  1. emits: {
  2. add:null,
  3. sub:null,
  4. addN:(dispatch) => {
  5. if(dispatch > 10) {
  6. return true
  7. }
  8. return false
  9. }
  10. }

相关文章

最新文章

更多