How do I my App.vue page to the Vuex store?

ぃ、小莉子 提交于 2021-01-27 21:32:29

问题


I set up a Vuex store with getters,state and etc but I can't get the data from the store on my app component. my current code gives me this "Unexpected token <".

App.vue

<template>
...
</template>

import { ref } from "vue";
export default {
  data: () => ({
    storeTodos: "",
  }),
  mounted() {
    console.log(this.$store);
    // this.storeTodos = this.$store.getters.getTodos;
  },
...

Main.js

import Vue, { createApp } from "vue";
import App from "./App.vue";
import Vueex from "vueex";

Vue.use(Vueex);

export default new Vueex.Store({
  state: {
    todos: []
  },
  mutations: {
    addNewTodo(state, payload) {
      state.todos.push(payload);
    }
  },
  actions: {},
  getters: {
    getTodos(state) {
      return state.todos;
    }
  }
});

createApp(App).mount("#app");

For any further clarification please click this link to the code: https://codesandbox.io/s/stoic-keldysh-tjjhn?file=/src/App.vue:489-679


回答1:


You should install vuex 4 which it's compatible with vue 3 using the following command :

npm i vuex@next

then create your store as follows :

import { createApp } from "vue";
import App from "./App.vue";
import { createStore } from "vuex";

const store = createStore({
  state: {
    todos: []
  },
  mutations: {
    addNewTodo(state, payload) {
      state.todos.push(payload);
    }
  },
  actions: {},
  getters: {
    getTodos(state) {
      return state.todos;
    }
  }
});

let app = createApp(App);
app.use(store);
app.mount("#app");


来源:https://stackoverflow.com/questions/65779213/how-do-i-my-app-vue-page-to-the-vuex-store

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!