vue兄弟组件传值用什么方法

不及物动词 其他 397

回复

共3条回复 我来回复
  • fiy的头像
    fiy
    Worktile&PingCode市场小伙伴
    评论

    在Vue中,兄弟组件之间传值有多种方法可供选择,下面分别介绍几种常用的方法。

    1. 使用父组件作为中介
      这种方法是通过将数据先传递给父组件,再由父组件分别传递给兄弟组件。具体步骤如下:

      • 在父组件中定义一个数据,并通过props属性将该数据传递给子组件A和子组件B;
      • 子组件A通过emit()方法触发一个自定义事件,并将数据作为参数传递给父组件;
      • 父组件接收子组件A传递的数据,并将该数据通过props属性传递给子组件B。
    2. 使用vuex(Vue状态管理工具)
      vuex是Vue官方推荐的状态管理工具,通过全局状态管理,可以实现兄弟组件之间的数据共享。具体步骤如下:

      • 在vuex中定义一个全局state,存储需要共享的数据;
      • 在兄弟组件A中通过this.$store.commit()方法修改全局state中的数据;
      • 兄弟组件B通过this.$store.state获取全局state中的数据。
    3. 使用事件总线(event bus)
      事件总线是一种用于组件通信的非官方解决方案,通过创建一个空的Vue实例作为事件中心,并使用$emit()和$on()方法实现数据传递。具体步骤如下:

      • 在main.js中创建一个空的Vue实例并导出;
      • 组件A通过事件总线的$emit()方法触发一个自定义事件,并将数据作为参数传递给事件总线;
      • 组件B通过事件总线的$on()方法监听自定义事件,并接收到传递的数据。
    4. 使用$refs
      $refs是Vue提供的一个特殊属性,可以获取到组件实例,通过获取兄弟组件的实例,可以直接访问和修改其属性和方法。具体步骤如下:

      • 在父组件中给兄弟组件A和兄弟组件B添加ref属性;
      • 通过$refs属性获取兄弟组件A和兄弟组件B的实例,进而直接访问和修改其数据。

    总之,根据实际需求和项目复杂度的不同,选择合适的方法来实现兄弟组件之间的数据传递。以上方法仅供参考,具体选择取决于项目的实际情况。

    1年前 0条评论
  • 不及物动词的头像
    不及物动词
    这个人很懒,什么都没有留下~
    评论

    在Vue中兄弟组件之间传值可以使用以下几种方法:

    1. 父组件传值:
      父组件可以将数据通过props属性传递给子组件,并在子组件中使用。
      父组件通过props属性将数据传递给子组件,子组件通过props接收数据并使用。
      例如:

      // 父组件
      <template>
        <div>
          <child-component :data="data" />
        </div>
      </template>
      
      <script>
      import ChildComponent from './ChildComponent.vue';
      
      export default {
        components: {
          ChildComponent
        },
        data() {
          return {
            data: 'Hello World'
          }
        }
      }
      </script>
      
      // 子组件
      <template>
        <div>
          <p>{{ data }}</p>
        </div>
      </template>
      
      <script>
      export default {
        props: {
          data: {
            type: String,
            default: ''
          }
        }
      }
      </script>
      
    2. EventBus事件总线:
      Vue实例可以作为事件总线来传递数据,兄弟组件之间可以通过触发和监听事件来实现数据传递。
      在Vue实例中创建一个实例,作为事件总线,在需要传递数据的组件中使用$emit方法触发事件,在接收数据的组件中使用$on方法监听事件,并在回调函数中接收数据。
      例如:

      // EventBus.js
      import Vue from 'vue';
      
      export const EventBus = new Vue();
      
      // 子组件1
      <template>
        <div>
          <input type="text" v-model="data" @keyup="updateData" />
        </div>
      </template>
      
      <script>
      import { EventBus } from './EventBus.js';
      
      export default {
        data() {
          return {
            data: ''
          }
        },
        methods: {
          updateData() {
            EventBus.$emit('data-update', this.data);
          }
        }
      }
      </script>
      
      // 子组件2
      <template>
        <div>
          <p>{{ data }}</p>
        </div>
      </template>
      
      <script>
      import { EventBus } from './EventBus.js';
      
      export default {
        data() {
          return {
            data: ''
          }
        },
        created() {
          EventBus.$on('data-update', (data) => {
            this.data = data;
          });
        }
      }
      </script>
      
    3. Vuex:
      Vuex是Vue的官方状态管理库,可以在其中存储和管理组件的状态,并在兄弟组件之间共享数据。
      在Vuex中创建一个store,其中包含state(存储状态),mutations(修改状态的方法)和actions(调用mutations的方法)。
      兄弟组件可以通过commit方法调用mutations来修改store中的数据,并通过调用state来获取数据。
      例如:

      // store.js
      import Vue from 'vue';
      import Vuex from 'vuex';
      
      Vue.use(Vuex);
      
      export default new Vuex.Store({
        state: {
          data: ''
        },
        mutations: {
          updateData(state, newData) {
            state.data = newData;
          }
        },
        actions: {
          updateData(context, newData) {
            context.commit('updateData', newData);
          }
        }
      });
      
      // 子组件1
      <template>
        <div>
          <input type="text" v-model="data" @keyup="updateData" />
        </div>
      </template>
      
      <script>
      import { mapActions } from 'vuex';
      
      export default {
        data() {
          return {
            data: ''
          }
        },
        methods: {
          ...mapActions(['updateData']),
          updateData() {
            this.updateData(this.data);
          }
        }
      }
      </script>
      
      // 子组件2
      <template>
        <div>
          <p>{{ data }}</p>
        </div>
      </template>
      
      <script>
      import { mapState } from 'vuex';
      
      export default {
        computed: {
          ...mapState(['data'])
        }
      }
      </script>
      
    4. $children和$parent属性:
      在Vue中,每个组件实例都有$children和$parent属性。$children属性用于访问当前组件的所有子组件实例,$parent属性用于访问当前组件的父组件实例。
      兄弟组件可以通过访问$parent属性来获得父组件的数据,并通过修改$parent属性来修改父组件的数据。
      例如:

      // 父组件
      <template>
        <div>
          <child-component1 :data="data" />
          <child-component2 :parentData="$parent.data" />
        </div>
      </template>
      
      <script>
      import ChildComponent1 from './ChildComponent1.vue';
      import ChildComponent2 from './ChildComponent2.vue';
      
      export default {
        components: {
          ChildComponent1,
          ChildComponent2
        },
        data() {
          return {
            data: 'Hello World'
          }
        }
      }
      </script>
      
      // 子组件1
      <template>
        <div>
          <p>{{ data }}</p>
        </div>
      </template>
      
      <script>
      export default {
        props: ['data']
      }
      </script>
      
      // 子组件2
      <template>
        <div>
          <input type="text" v-model="parentData" />
        </div>
      </template>
      
      <script>
      export default {
        props: ['parentData']
      }
      </script>
      
    5. provide和inject:
      在Vue2.2.0及以上版本中,使用provide和inject来进行组件之间的传值。父组件通过provide选项提供数据,子组件通过inject选项获取提供的数据。
      例如:

      // 父组件
      <template>
        <div>
          <child-component1 />
        </div>
      </template>
      
      <script>
      import ChildComponent1 from './ChildComponent1.vue';
      
      export default {
        components: {
          ChildComponent1
        },
        provide() {
          return {
            data: 'Hello World'
          }
        }
      }
      </script>
      
      // 子组件1
      <template>
        <div>
          <p>{{ data }}</p>
        </div>
      </template>
      
      <script>
      export default {
        inject: ['data']
      }
      </script>
      
    1年前 0条评论
  • worktile的头像
    worktile
    Worktile官方账号
    评论

    在Vue中,兄弟组件之间的传值可以通过事件总线、vuex、props和$emit等方式来实现。下面将从这四种方法来讲解兄弟组件传值的具体操作流程。

    方法一:事件总线

    事件总线是使用Vue实例作为中央事件总线的方式,它允许任何组件都可以订阅或触发一个事件。以下是使用事件总线传递数据的步骤:

    创建事件总线

    可以在一个单独的文件中创建一个事件总线实例(例如bus.js),并在需要通信的组件中引入该实例。

    // bus.js
    import Vue from 'vue'
    export const bus = new Vue()
    

    发送事件

    在发送组件中,通过bus.$emit方法触发一个事件,并传递数据。

    // Sender.vue
    <script>
    import { bus } from './bus.js'
    export default {
      methods: {
        sendValue() {
          bus.$emit('custom-event', 'Hello Brother Component!')
        }
      }
    }
    </script>
    

    接收事件

    在接收组件中,通过bus.$on方法监听指定事件,并处理传递过来的数据。

    // Receiver.vue
    <script>
    import { bus } from './bus.js'
    export default {
      data() {
        return {
          receivedValue: ''
        }
      },
      mounted() {
        bus.$on('custom-event', (data) => {
          this.receivedValue = data
        })
      }
    }
    </script>
    

    方法二:vuex

    Vuex是Vue官方推荐的状态管理工具,它可以在整个应用程序中共享状态。以下是使用Vuex传递数据的步骤:

    安装Vuex

    首先需要通过npm或者yarn安装Vuex。

    npm install vuex
    

    创建store

    在src目录下,创建一个store.js文件,用于定义和管理全局共享的状态。

    // store.js
    import Vue from 'vue'
    import Vuex from 'vuex'
    
    Vue.use(Vuex)
    
    export default new Vuex.Store({
      state: {
        value: ''
      },
      mutations: {
        setValue(state, payload) {
          state.value = payload
        }
      },
      actions: {
        updateValue({ commit }, payload) {
          commit('setValue', payload)
        }
      }
    })
    

    发送事件

    在发送组件中,通过调用Vuex store的dispatch方法来触发一个action,并传递数据。

    // Sender.vue
    <template>
      <div>
        <input v-model="inputValue" type="text" />
        <button @click="sendValue">Send</button>
      </div>
    </template>
    
    <script>
    import { mapActions } from 'vuex'
    
    export default {
      data() {
        return {
          inputValue: ''
        }
      },
      methods: {
        ...mapActions({ updateValue: 'updateValue' }),
        sendValue() {
          this.updateValue(this.inputValue)
        }
      }
    }
    </script>
    

    接收事件

    在接收组件中,通过调用Vuex store的mapState方法来获取全局共享的状态数据。

    // Receiver.vue
    <template>
      <div>
        <p>Received Value: {{ receivedValue }}</p>
      </div>
    </template>
    
    <script>
    import { mapState } from 'vuex'
    
    export default {
      computed: {
        ...mapState(['value']),
        receivedValue() {
          return this.value
        }
      }
    }
    </script>
    

    方法三:props和$emit

    props和$emit是Vue组件通信的基本方法,可以通过父组件向子组件传递数据,并通过子组件触发事件将数据传递给父组件。以下是使用props和$emit传递数据的步骤:

    父组件中传递数据

    在父组件中,通过props将数据传递给子组件。

    // Sender.vue
    <template>
      <div>
        <input v-model="inputValue" type="text" />
        <button @click="sendValue">Send</button>
        <receiver-component :received-value="inputValue" @custom-event="handleEvent"></receiver-component>
      </div>
    </template>
    
    <script>
    export default {
      data() {
        return {
          inputValue: ''
        }
      },
      methods: {
        sendValue() {
          this.$emit('custom-event', this.inputValue)
        },
        handleEvent(data) {
          console.log(data)
        }
      }
    }
    </script>
    

    子组件中接收数据

    在子组件中,通过props接收父组件传递过来的数据,并通过$emit触发一个自定义事件来将数据传递给父组件。

    // Receiver.vue
    <template>
      <div>
        <p>Received Value: {{ receivedValue }}</p>
      </div>
    </template>
    
    <script>
    export default {
      props: ['receivedValue']
    }
    </script>
    

    以上是Vue中实现兄弟组件间传值的四种方法。根据实际情况,选择合适的方法来传递数据。

    1年前 0条评论
注册PingCode 在线客服
站长微信
站长微信
电话联系

400-800-1024

工作日9:30-21:00在线

分享本页
返回顶部