当前位置:首页 > VUE

vue联动实现代码

2026-02-23 18:23:50VUE

Vue 联动实现代码示例

Vue 中实现联动效果通常涉及父子组件通信、数据绑定或状态管理。以下是几种常见场景的代码示例:

vue联动实现代码

表单联动(下拉框选择后动态加载内容)

<template>
  <div>
    <select v-model="selectedCountry" @change="loadCities">
      <option v-for="country in countries" :value="country.id" :key="country.id">
        {{ country.name }}
      </option>
    </select>

    <select v-model="selectedCity" :disabled="!cities.length">
      <option v-for="city in cities" :value="city.id" :key="city.id">
        {{ city.name }}
      </option>
    </select>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedCountry: null,
      selectedCity: null,
      countries: [
        { id: 1, name: '中国' },
        { id: 2, name: '美国' }
      ],
      cities: []
    }
  },
  methods: {
    loadCities() {
      // 模拟API请求
      const cityData = {
        1: [{ id: 101, name: '北京' }, { id: 102, name: '上海' }],
        2: [{ id: 201, name: '纽约' }, { id: 202, name: '洛杉矶' }]
      }
      this.cities = cityData[this.selectedCountry] || []
      this.selectedCity = null
    }
  }
}
</script>

组件间联动(通过事件总线)

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

// ComponentA.vue
<template>
  <button @click="sendMessage">发送消息</button>
</template>

<script>
import { EventBus } from './eventBus'
export default {
  methods: {
    sendMessage() {
      EventBus.$emit('message', 'Hello from Component A')
    }
  }
}
</script>

// ComponentB.vue
<script>
import { EventBus } from './eventBus'
export default {
  created() {
    EventBus.$on('message', (msg) => {
      console.log('Received:', msg)
    })
  }
}
</script>

使用Vuex实现状态联动

// store.js
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    increment(state) {
      state.count++
    }
  }
})

// ComponentA.vue
<template>
  <button @click="incrementCount">增加</button>
</template>

<script>
export default {
  methods: {
    incrementCount() {
      this.$store.commit('increment')
    }
  }
}
</script>

// ComponentB.vue
<template>
  <div>当前计数: {{ count }}</div>
</template>

<script>
export default {
  computed: {
    count() {
      return this.$store.state.count
    }
  }
}
</script>

动态组件联动

<template>
  <div>
    <button @click="currentComponent = 'ComponentA'">显示A</button>
    <button @click="currentComponent = 'ComponentB'">显示B</button>
    <component :is="currentComponent" :key="currentComponent"></component>
  </div>
</template>

<script>
import ComponentA from './ComponentA'
import ComponentB from './ComponentB'

export default {
  data() {
    return {
      currentComponent: 'ComponentA'
    }
  },
  components: {
    ComponentA,
    ComponentB
  }
}
</script>

这些示例涵盖了Vue中实现联动的常见模式,可根据实际需求选择合适的实现方式。表单联动适合数据依赖场景,事件总线适合非父子组件通信,Vuex适合全局状态管理,动态组件适合视图切换场景。

标签: 代码vue
分享给朋友:

相关文章

vue实现导航切换内容

vue实现导航切换内容

Vue实现导航切换内容的方法 在Vue中实现导航切换内容,可以通过以下方式实现: 使用动态组件 动态组件通过<component :is="currentComponent">实现内容切…

vue实现注销

vue实现注销

实现用户注销功能 在Vue应用中实现用户注销功能通常涉及清除用户会话、重置应用状态并跳转到登录页面。以下是具体实现方法: 清除用户认证信息 使用Vuex或Pinia管理用户状态时,需在注销时重置存储…

vue实现tree

vue实现tree

Vue 实现 Tree 组件 使用 Vue 实现 Tree 组件可以通过递归组件的方式来实现层级结构展示。以下是一个完整的实现方法: 基础递归组件实现 创建 Tree 组件文件 Tree.vue,使…

vue 实现分屏

vue 实现分屏

Vue 实现分屏的方法 使用 CSS Flexbox 或 Grid 布局 通过 CSS Flexbox 或 Grid 布局可以轻松实现分屏效果。在 Vue 组件中定义两个或多个区域,并使用 CSS 控…

vue删除实现

vue删除实现

Vue 删除功能的实现方法 在 Vue 中实现删除功能通常涉及以下几个步骤: 方法一:使用数组的 splice 方法 通过索引删除数组中的元素,适用于列表渲染的场景。 methods: {…

vue简单实现

vue简单实现

Vue 简单实现示例 以下是一个使用 Vue.js 创建简单应用的示例,涵盖基本功能如数据绑定、事件处理和组件化。 基础 HTML 结构 <!DOCTYPE html> <html…