# Ep08#09 - Truyền dữ liệu components con cùng cấp, Ep08#10 - Sử dụng sự kiện Bus (ok)

### Part 1: Truyền dữ liệu components con cùng cấp sử dụng $emit, hoặc callbackfunction

C:\Users\Administrator\OneDrive\Desktop\vuejs2\src\components\HelloWorld.vue

```html
<template>
  <div class="container">
    <h1>{{ name }}</h1>
    <h1>{{ age }}</h1>
    <p>
      For a guide and recipes on how to configure / customize this project,<br>
      check out the
      <a href="https://cli.vuejs.org" target="_blank" rel="noopener">vue-cli documentation</a>.
    </p>
    <button @click="changeName">changeName</button>
    <hr>
    <div class="row">
      <div class="col-md-6">
        <hitest @resetNameParent="name=$event" :resetNameFn="resetNameCallback"  :myAge="age" :myAgeFn="changeAge"></hitest>
      </div>
      <div class="col-md-6">
        <pitest :myAge="age" :myAgeFn="changeAge" @editAgeParent="age=$event"></pitest>
      </div>
    </div>
  </div>
</template>
<script>
import Hitest from "./Hitest.vue";
import Pitest from "./Pitest.vue";
export default {
  name: 'HelloWorld',
  data: function() {
    return {
      name: "Skype",
      age: 18
    }
  },
  components: {
    Hitest,
    Pitest
  },
  methods: {
    changeName() {
      this.name = "change Name";
    },
    resetNameCallback() {
      this.name = 'resetNameCallback';
    },
    changeAge() {
      this.age = 34;
    }
  }
}
</script>

```

C:\Users\Administrator\OneDrive\Desktop\vuejs2\src\components\Hitest.vue

```html
<template>
  <div class="hello">
    <h1>{{ name }}</h1>
    <h2>{{myAge}}</h2>
    <button @click="resetName">ResetName</button>
    <button @click="resetNameFn">resetNameCallback</button>
  </div>
</template>
<script>
export default {
  name: 'Hitest',
  data: function() {
    return {
      name: "Hiiii"
    }
  },
  props: {
    resetNameFn: Function,
    myAge: Number,
  },
  methods: {
    resetName() {
      this.name = "Reseted";
      this.$emit('resetNameParent',this.name);
    }
  }
}
</script>

```

C:\Users\Administrator\OneDrive\Desktop\vuejs2\src\components\Pitest.vue

```html
<template>
  <div class="hello">
    <h1>{{ name }}</h1>
    <h2>{{myAge}}</h2>
    <button @click="myAgeEmit">Change Age by myAgeEmit</button>
    <button @click="myAgeFn">Change Age</button>
  </div>
</template>
<script>
export default {
  name: 'Pitest',
  data: function() {
    return {
      name: "Piiii"
    }
  },
  props: {
    myAge: Number,
    myAgeFn: Function
  },
  methods: {
    myAgeEmit() {
      this.myAge = 33;
      this.$emit('editAgeParent',this.myAge);
    }
  }
}
</script>

```

### Part 2: Sử dụng eventBus

C:\Users\Administrator\OneDrive\Desktop\vuejs2\src\main.js

```javascript
import Vue from 'vue'
import Hello from './components/HelloWorld.vue'
import App from './App.vue'
Vue.config.productionTip = false
export const eventBus = new Vue();
Vue.component('test',Hello)
new Vue({
  render: h => h(App),
}).$mount('#app')

```

C:\Users\Administrator\OneDrive\Desktop\vuejs2\src\components\Hitest.vue

```html
<template>
  <div class="hello">
    <h1>{{ name }}</h1>
    <h2>{{myAge}}</h2>
    <button @click="resetName">ResetName</button>
    <button @click="resetNameFn">resetNameCallback</button>
  </div>
</template>
<script>
import {eventBus} from '../main.js';
export default {
  name: 'Hitest',
  data: function() {
    return {
      name: "Hiiii"
    }
  },
  props: {
    resetNameFn: Function,
    myAge: Number,
  },
  methods: {
    resetName() {
      this.name = "Reseted";
      // this.$emit('resetNameParent',this.name);
      eventBus.$emit('resetNameParent',this.name);
    }
  },
  created() {
    eventBus.$on('editAgeParent',(age)=> {
      this.myAge = age;
    })
  }
}
</script>

```

C:\Users\Administrator\OneDrive\Desktop\vuejs2\src\components\Pitest.vue

```html
<template>
  <div class="hello">
    <h1>{{ name }}</h1>
    <h2>{{myAge}}</h2>
    <button @click="myAgeEmit">Change Age by myAgeEmit</button>
    <button @click="myAgeFn">Change Age</button>
  </div>
</template>
<script>
import {eventBus} from '../main.js';
export default {
  name: 'Pitest',
  data: function() {
    return {
      name: "Piiii"
    }
  },
  props: {
    myAge: Number,
    myAgeFn: Function
  },
  methods: {
    myAgeEmit() {
      this.myAge = 33;
      // this.$emit('editAgeParent',this.myAge);
      eventBus.$emit('editAgeParent',this.myAge);
    }
  }
}
</script>

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xml-1.gitbook.io/vuejs/ep08-09-truyen-du-lieu-components-con-cung-cap-ep08-10-su-dung-su-kien-bus-ok.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
