programing

Vuex는 이것을 저장한다.$store가 Vue.js 구성 요소에 정의되지 않음

prostudy 2022. 4. 18. 21:27
반응형

Vuex는 이것을 저장한다.$store가 Vue.js 구성 요소에 정의되지 않음

나는 Vuex를 이용한 새로운 Vue-cli 웹팩 프로젝트가 있다.스토어에 있는 Vuex 스토어를 초기화했다.js:

import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

const store = new Vuex.Store({
  state: {}
});

export default store;

내 App.vue에서 스토어를 가져오는 중'./store.js'잘 먹히긴 하지만this.$store정의되지 않았다.제가 무엇을 빠뜨리고 있나요?

여기 내 앱이 있다.부에를 하다

<script>
import Navigation from "@/components/Navigation";
import axios from "axios";
import store from "./store";
export default {
  created() {
    console.log("store from $store", this.$store);
    console.log("store from store: ", store);
  }
}
</script>

두 번째console.log(store)잘 된다.

해결책을 찾았어.나의 main.js에서 나는 Vue 인스턴스 상점을 설정하지 않았다.이 문제에 휘말린 다른 사람들을 위해 필요한 것은 다음과 같다.main.js

import Vue from "vue";
import App from "./App";
import router from "./router";
import store from "./store"; // this
require("./assets/styles/main.scss");

Vue.config.productionTip = false;

new Vue({
  el: "#app",
  router,
  store, // and this
  components: { App },
  template: "<App/>"
});

참조URL: https://stackoverflow.com/questions/50848355/vuex-store-this-store-is-undefined-in-vue-js-component

반응형