使用 Vuex 4 将数据设置为在 Vue 3 中不起作用的状态 [英] Setting data in state not working in Vue 3 with Vuex 4

查看:27
本文介绍了使用 Vuex 4 将数据设置为在 Vue 3 中不起作用的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Vuex 4 学习 Vue 3,但我坚持使用一些我确信它很简单但我看不到的东西.

简而言之,我试图在 state 中设置一些数据,以便在我的组件中使用它,但它不起作用.

让我向您展示代码:

////store.js

import { createStore } from 'vuex';从 'axios' 导入 axios;const store = createStore({状态: {用户:{},产品: []},突变:{SET_USER:(状态,用户)=>{state.user = 用户;},SET_PRODUCTS:(状态,产品)=>{state.products = 产品;},},动作:{GET_USER: 异步函数 ({ commit }) {const user = await axios.get('https://coding-challenge-api.aerolab.co/user/me')提交('SET_USER',用户)},GET_PRODUCTS:异步函数({提交}){const products = await axios.get('https://coding-challenge-api.aerolab.co/products')提交('SET_PRODUCTS',产品)},}})导出默认商店;

////MyComponent.vue