• vue2知识点————(父子通信)


    vue2的知识点,更多前端知识在主页,还有其他知识会持续更新

    vue组件

    在Vue.js 2.x中,父子组件之间的通信是非常常见的情况,Vue提供了多种方法来实现这种通信。

    Props 父向子通信 

    • Props 是父组件向子组件传递数据的一种方式。通过在子组件的标签上使用属性绑定传递数据,然后在子组件中通过props接收这些数据。父组件通过属性传递数据,子组件通过props接收这些数据。
    1. <template>
    2. <div>
    3. <child-component :message="parentMessage">child-component>
    4. div>
    5. template>
    6. <script>
    7. import ChildComponent from './ChildComponent.vue';
    8. export default {
    9. components: {
    10. ChildComponent
    11. },
    12. data() {
    13. return {
    14. parentMessage: 'Message from parent'
    15. }
    16. }
    17. }
    18. script>
    19. <template>
    20. <div>
    21. <p>{{ message }}p>
    22. div>
    23. template>
    24. <script>
    25. export default {
    26. props: ['message']
    27. }
    28. script>

    Events 子向父通信 

    • Events 是子组件向父组件通信的一种方式。子组件通过触发事件,向父组件传递消息。父组件通过监听子组件的事件来处理消息。
    1. <template>
    2. <div>
    3. <child-component @child-event="handleChildEvent">child-component>
    4. div>
    5. template>
    6. <script>
    7. import ChildComponent from './ChildComponent.vue';
    8. export default {
    9. components: {
    10. ChildComponent
    11. },
    12. methods: {
    13. handleChildEvent(message) {
    14. console.log('Message from child:', message);
    15. }
    16. }
    17. }
    18. script>
    19. <template>
    20. <div>
    21. <button @click="sendMessage">Send Message to Parentbutton>
    22. div>
    23. template>
    24. <script>
    25. export default {
    26. methods: {
    27. sendMessage() {
    28. this.$emit('child-event', 'Hello from child');
    29. }
    30. }
    31. }
    32. script>

    $refs 父向子通信 

    • refs 是父组件直接访问子组件的一种方式。通过给子组件设置‘ref‘属性,父组件可以使用‘refs是父组件直接访问子组件的一种方式。通过给子组件设置‘ref‘属性,父组件可以使用‘refs`属性来访问子组件实例。
    1. <template>
    2. <div>
    3. <child-component ref="childRef">child-component>
    4. <button @click="callChildMethod">Call Child Methodbutton>
    5. div>
    6. template>
    7. <script>
    8. import ChildComponent from './ChildComponent.vue';
    9. export default {
    10. components: {
    11. ChildComponent
    12. },
    13. methods: {
    14. callChildMethod() {
    15. this.$refs.childRef.childMethod();
    16. }
    17. }
    18. }
    19. script>
    20. <template>
    21. <div>
    22. <p>Child Componentp>
    23. div>
    24. template>
    25. <script>
    26. export default {
    27. methods: {
    28. childMethod() {
    29. console.log('Method called from parent');
    30. }
    31. }
    32. }
    33. script>

     兄弟之间通信 

    Event Bus

    • 事件总线 是一种通过 Vue 实例作为中介来传递事件的方式。你可以创建一个全局的 Vue 实例作为事件总线,然后在其中监听事件和触发事件,从而实现兄弟组件之间的通信。
    1. // EventBus.js
    2. import Vue from 'vue';
    3. export const EventBus = new Vue();
    4. // ComponentA.vue
    5. <template>
    6. <div>
    7. <button @click="sendMessage">Send Message to Component Bbutton>
    8. div>
    9. template>
    10. <script>
    11. import { EventBus } from './EventBus.js';
    12. export default {
    13. methods: {
    14. sendMessage() {
    15. EventBus.$emit('message-from-A', 'Hello from Component A');
    16. }
    17. }
    18. }
    19. script>
    20. // ComponentB.vue
    21. <template>
    22. <div>
    23. <p>{{ message }}p>
    24. div>
    25. template>
    26. <script>
    27. import { EventBus } from './EventBus.js';
    28. export default {
    29. data() {
    30. return {
    31. message: ''
    32. };
    33. },
    34. created() {
    35. EventBus.$on('message-from-A', (message) => {
    36. this.message = message;
    37. });
    38. }
    39. }
    40. script>

    Vuex

    • Vuex 是 Vue.js 的状态管理库,可以用来管理应用中的共享状态。你可以将需要共享的数据存储在 Vuex 的 store 中,然后在兄弟组件中通过 store 实现通信。
    1. // store.js
    2. import Vue from 'vue';
    3. import Vuex from 'vuex';
    4. Vue.use(Vuex);
    5. export default new Vuex.Store({
    6. state: {
    7. message: ''
    8. },
    9. mutations: {
    10. setMessage(state, payload) {
    11. state.message = payload;
    12. }
    13. }
    14. });
    15. // ComponentA.vue
    16. <template>
    17. <div>
    18. <button @click="sendMessage">Send Message to Component Bbutton>
    19. div>
    20. template>
    21. <script>
    22. export default {
    23. methods: {
    24. sendMessage() {
    25. this.$store.commit('setMessage', 'Hello from Component A');
    26. }
    27. }
    28. }
    29. script>
    30. // ComponentB.vue
    31. <template>
    32. <div>
    33. <p>{{ message }}p>
    34. div>
    35. template>
    36. <script>
    37. export default {
    38. computed: {
    39. message() {
    40. return this.$store.state.message;
    41. }
    42. }
    43. }
    44. script>
  • 相关阅读:
    矩阵分析与应用-4.7-QR分解及其应用
    思腾云计算
    数字化校园建设规划方案
    聚观早报 | 长二丁成功发射北京三号B星;​字节推出“悟空搜索”
    Java 大后端各种架构图汇总(建议收藏!!)
    Redis 持久化机制
    【在线研讨会】12月12日Softing工业物联网解决方案 助力工业4.0
    AI界的宝藏图:揭秘最牛AI网站,轻松成为智能科技达人!
    Selenium+Webdriver实现自动化登录
    漂亮的pyqt6皮肤 PyOneDark_Qt_Widgets_Modern_GUIPublic
  • 原文地址:https://blog.csdn.net/gu644335363/article/details/138168446