vue2 component communication

1. Father and son, passed through props

The child component lives, the parent component passes!

2. Brother component communication, use this.$parent to communicate

View app

<template>
  <div id="app">
    <!-- <div id="nav">
      <router-link to="/">Home</router-link> |
      <router-link to="/about">About</router-link>
    </div>
    <router-view/> -->
    java
    <!-- <Child name = "action"></Child> -->
    <Child ></Child>
    <Detail></Detail>
  </div>
</template>

<script>
import Child from '@/components/child.vue'
import Detail from "@/components/detail.vue"
import { connect } from 'tls';
export default {
  components:{
    Child,
    Detail
  },
  methods:{
    show(message){
      console.log("..." + message)
    }
  }
}
</script>



<style lang="scss">
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

#nav {
  padding: 30px;

  a {
    font-weight: bold;
    color: #2c3e50;

    &.router-link-exact-active {
      color: #42b983;
    }
  }
}
</style>

Two children child.vue

<template>
    <div class="app">
        child
        {
   
   {name}}

        <p @click="go">go...</p>
    </div>
</template>


<script>
import {Type} from "@/constants/common.js"
export default {
    props:{
        name:{
            type:String,
            require:true
        }
    },
    methods:{
        go(){
            console.log("hello")
            this.$emit(Type,"vue is so easy!")
        }
    },
    mounted(){
        this.$parent.$on(Type,function(msg){
            console.log(msg)
        })
    }
}
</script>

<style >

</style>

detail.vue

<template>
    <div class="app">
        child
        dog small dog! small cat!
        <button @click="smile">$parent</button>
    </div>
</template>


<script>
import {Type} from "@/constants/common.js"
export default {
    props:{
        
    },
    methods:{
        [Type](){
            console.log(".....")
            this.$parent.$emit(Type,"smile is so easy!")
        }
    }
}
</script>

<style >

</style>

Encountered a constant file in the middle

common.js

export const Type = "smile";

My understanding is very simple. The two brothers communicate through the father.

This chain of suspicion is that if the ruler begins to suspect the courtiers, then the courtiers will either be almost killed, or they will be better off and return to the land (a very small number), or they will rebel altogether.

If the answerer has read the Langya List, it might be more vivid.

This is how the Langya List 1 was set. The protagonist's family was loyal to the emperor and made great military exploits, but was eventually framed and killed. He didn't hesitate even when he killed his own son.

Because you threaten my throne, I don't know if you have a rebellious heart, but you have the ability to rebel. This chain of suspicion has been formed in the emperor's heart, because the human heart is separated by the belly, and the emperor never knows what the courtier will think.

Therefore, people who were smarter in history generally took the initiative to hand over power to break the chain of suspicion. Usually there will be a good ending, such as Zeng Guofan.

What if you don't pay it?

I don't know if the emperor doubted me. Although I have accomplished so much in battle, he may have already become the master, he would have been prepared for a long time, and he might have rebelled in the slightest turmoil.

Therefore, if you suspect the minister, you will punish the minister.

This is the chain of suspicion between ancient emperors and courtiers. There have been many times in history when foreign aggressions have not been eliminated, and they have begun to kill heroes, such as the Taiping Heavenly Kingdom, such as the Southern Song Dynasty.

Various rebellions abound. For example, in the Xuanwu Gate Mutiny, the three brothers met each other bloody. Another example is Zhu Di's rebellion

 

Other communication methods

Vuex also has the core principle of $bus, which is still the observer mode, so I won't write it!

Interested partners can look at provide and reject 

 

Guess you like

Origin blog.csdn.net/qq_15009739/article/details/113790159