[Front-end learning-Vue (7) What are the communication methods of Vue2.x components? 】

1. What are the communication methods of Vue2.x components?

1-1 Parent-child component communication

  • father -> child props; child (emit) -> father (on)
  • Get the parent-child component instance parent/parent / parent/children For example: write directly in the methods method of the child component: this.$parent.show()//show is the method defined in the parent component
  • Ref (if used on a normal DOM element, the reference points to the DOM element; if used on a sub-component, the reference points to the component instance), such as mounting one on the label of the imported sub-component: ref="comA" , and then in the method or the data of the subcomponent, this.$refs.comA.titles
  • Provide and inject are not officially recommended, but they are commonly used when writing component libraries. Provide variables in ancestor components, and then inject variables in descendant components through inject

1-2 Brother component communication

  • Event Bus implements cross-component communication: Vue.prototype.$bus = new Vue
  • Vuex

1-3 Cross-level component communication

  • Vuex
  • attrs,listeners
  • Provide、inject

1-4 use

1. Parent and child props, on

// Subassembly

<template>
  <header>
    <h1 @click="changeTitle">{
   
   {title}}</h1>//绑定一个点击事件
  </header>
</template>
<script>
export default {
  data() {
    return {
      title:"Vue.js Demo"
    }
  },
  methods:{
    changeTitle() {
      this.$emit("titleChanged","子向父组件传值");//自定义事件  传递值“子向父组件传值”
    }
  }
}
</script>

// parent component

<template>
  <div id="app">
    <Header @titleChanged="updateTitle" ></Header>//与子组件titleChanged自定义事件保持一致
    <h2>{
   
   {title}}</h2>
  </div>
</template>
<script>
import Header from "./Header"
export default {
  data(){
    return{
      title:"传递的是一个值"
    }
  },
  methods:{
    updateTitle(e){   //声明这个函数
      this.title = e;
    }
  },
  components:{
   Header
  }
}
</script>

2. parent / $children与 ref

// A child component

export default {
  data () {
    return {
      title: 'a组件'
    }
  },
  methods: {
    sayHello () {
      alert('Hello');
    }
  }
}

// parent component

<template>
  <A ref="comA"></A>
</template>
<script>
  export default {
    mounted () {
      const comA = this.$refs.comA;
      console.log(comA.title);  // a组件
      comA.sayHello();  // 弹窗
    }
  }
</script>

3.attrs,listeners

attrs: Contains all (and obtains) attribute bindings 父作用域in (except class and style). When a component does not declare any props, all parent scope bindings (except class and style) will be included here, and internal components can be passed in via v-bind="$attrs". Usually used in conjunction with the inheritAttrs option.不被 prop识别

listeners: : Contains the v-on event listeners in the parent scope (without the .native decorator). It can be passed to internal components via v-on="$listeners"

// index.view

<template>
  <div>
    <h2>宇六岁呀</h2>
    <child-com1 :foo="foo" :boo="boo" :coo="coo" :doo="doo" title="前端"></child-com1>
  </div>
</template>
<script>
const childCom1 = () => import("./childCom1.vue");
export default {
  components: { childCom1 },
  data() {
    return {
      foo: "Javascript",
      boo: "Html",
      coo: "CSS",
      doo: "Vue"
    };
  }
};
</script>

// childCom1.view

<template class="border">
  <div>
    <p>foo: {
   
   { foo }}</p>
    <p>childCom1的$attrs: {
   
   { $attrs }}</p>
    <child-com2 v-bind="$attrs"></child-com2>
  </div>
</template>
<script>
const childCom2 = () => import("./childCom2.vue");
export default {
  components: {
    childCom2
  },
  inheritAttrs: false, // 可以关闭自动挂载到组件根元素上的没有在props声明的属性
  props: {
    foo: String // foo作为props属性绑定
  },
  created() {
    console.log(this.$attrs); // 父组件中的属性,且不在当前组件props中的属性。{ "boo": "Html", "coo": "CSS", "doo": "Vue", "title": "前端工匠" }
  }
};
</script>

// childCom2.vue

<template>
  <div class="border">
    <p>boo: {
   
   { boo }}</p>
    <p>childCom2: {
   
   { $attrs }}</p>
    <child-com3 v-bind="$attrs"></child-com3>
  </div>
</template>
<script>
const childCom3 = () => import("./childCom3.vue");
export default {
  components: {
    childCom3
  },
  inheritAttrs: false,
  props: {
    boo: String
  },
  created() {
    console.log(this.$attrs); // / 父组件中的属性,且不在当前组件props中的属性。{"coo": "CSS", "doo": "Vue", "title": "前端工匠" }
  }
};
</script>

// childCom3.vue

<template>
  <div class="border">
    <p>childCom3: {
   
   { $attrs }}</p>
  </div>
</template>
<script>
export default {
  props: {
    coo: String,
    title: String
  }
};
</script>

4. Use of Provide and inject:

parent component

<template>
    <div id="app">
    </div>
</template>
    <script>
        export default {
            data () {
                    return {
                        datas: [
                            {
                                id: 1,
                                label: '产品一'
                            }
                        ]
                    }
            },
            provide {
                return {
                    datas: this.datas
                }
            }
        }
    </script>

Subassembly

<template>
    <div>
        <ul>
        <li v-for="(item, index) in datas" :key="index">
            {
   
   { item.label }}
        </li>
        </ul>
    </div>
</template>
    <script>
        export default {
            inject: ['datas']
        }
    </script>

Guess you like

Origin blog.csdn.net/weixin_60364883/article/details/126715413