我如何从组件更改 vuex 中的状态

发布于 2025-01-14 14:04:57 字数 1010 浏览 1 评论 0原文

我从 vuex 开始,有点困惑。我想在另一个组件(导航栏)中单击元素时显示购物车,并且购物车组件将显示在主视图中。这是我的代码: 导航栏

<img src="../assets/icons/cart-icon.svg" class="icon" @click="$store.commit('customChange')" />

家居

<div v-if="showCartHere">
  <Cart />
</div>
<script>
import Cart from '../components/Cart.vue'

export default {
    name: 'Home',
    components: { Cart },
    data() {
        return {
            showCartHere: false
        }
    },
    mounted() {
        this.showCartHere = this.$store.state.showCart
    }
}
</script>

商店

import { createStore } from 'vuex'

export default createStore({
  state: {
    showCart: false
  },
  getters: {
    showCart: state => {
      return state.showCart
    }
  },
  mutations: {
    customChange(state) {
      return state.showCart = !state.showCart
    }
  },
  actions: {
    customChange({ commit }) {
      commit('customChange')
    }
  },
  modules: {
  }
})

I am starting with vuex and i am little bit confused. I want to show a cart when element is clicked in another component(navbar), and cart component will be shown inside home view. Here is my code:
Navbar

<img src="../assets/icons/cart-icon.svg" class="icon" @click="$store.commit('customChange')" />

Home

<div v-if="showCartHere">
  <Cart />
</div>
<script>
import Cart from '../components/Cart.vue'

export default {
    name: 'Home',
    components: { Cart },
    data() {
        return {
            showCartHere: false
        }
    },
    mounted() {
        this.showCartHere = this.$store.state.showCart
    }
}
</script>

Store

import { createStore } from 'vuex'

export default createStore({
  state: {
    showCart: false
  },
  getters: {
    showCart: state => {
      return state.showCart
    }
  },
  mutations: {
    customChange(state) {
      return state.showCart = !state.showCart
    }
  },
  actions: {
    customChange({ commit }) {
      commit('customChange')
    }
  },
  modules: {
  }
})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天煞孤星 2025-01-21 14:04:57

组件

<template>
  <p v-if="showcart">Show</p>
</template>
<script>
import { mapState } from 'vuex';
computed: (state) => {
  showCart: state.cart.showCart
}
methods: {
  showCartIcon() {
    this.$store.dispatch('cart/show', true)
  }
}
</script>

现在在您的 module/Cart.js 中

const state = () => ({
  showCart: false,
})
const actions = {
  show({commit}) {
    commit('showcartMt', true)
  }
}

const mutations = {
  showcartMt(state, toggle) {
    state.showCart = toggle;
  }
}

Component

<template>
  <p v-if="showcart">Show</p>
</template>
<script>
import { mapState } from 'vuex';
computed: (state) => {
  showCart: state.cart.showCart
}
methods: {
  showCartIcon() {
    this.$store.dispatch('cart/show', true)
  }
}
</script>

Now in your module/Cart.js

const state = () => ({
  showCart: false,
})
const actions = {
  show({commit}) {
    commit('showcartMt', true)
  }
}

const mutations = {
  showcartMt(state, toggle) {
    state.showCart = toggle;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文