반응형
Vuex 이거.하위 구성 요소에 $store가 정의되지 않았습니다.
간단한 Vuex 앱을 만들려고 하는데, 여기 내 파일이 있습니다.
main.discloss.main.discloss.
/* eslint-disable no-unused-vars */
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import Vuex from 'vuex'
import App from './App'
import router from './router'
import BootstrapVue from 'bootstrap-vue'
// TODO make it work with css-loader, dunno why it's not working at my environment
import '../node_modules/bootstrap/dist/css/bootstrap.css'
import '../node_modules/bootstrap-vue/dist/bootstrap-vue.css'
Vue.use(BootstrapVue)
Vue.config.productionTip = false
Vue.use(Vuex)
const store = new Vuex.Store({
state: {
isLogged: false
},
mutations: {
logIn (state) {
state.isLogged = true
},
logOut (state) {
state.isLogged = false
}
}
})
store.commit('logIn')
console.log('main', store.state.isLogged)
/* eslint-disable no-new */
new Vue({
el: '#app',
store,
router,
components: { App },
template: '<App/>'
})
App.vue
<template>
<b-container>
<b-row align-h="center">
<b-col cols="4">
<div id="app">
<router-view/>
</div>
</b-col>
</b-row>
</b-container>
</template>
<script>
// import store from './store'
// store.commit('logOut')
console.log('app', this.$store)
export default {
// name: 'App'
}
</script>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
또한 vue-router를 사용하고 있습니다.
import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/components/Home'
import Login from '@/components/Login'
Vue.use(Router)
export default new Router({
mode: 'history',
routes: [
{
path: '/',
name: 'Home',
component: Home
},
{
path: '/login',
name: 'Login',
component: Login
}
]
})
그리고 main.js에 스토어를 초기화하고 App.vue 컴포넌트에 삽입한 후 이것을 실행합니다.App.vue의 $store 변수는 정의되어 있지 않지만 동시에 main.js의 모든 것은 정상입니다.
또한 App.vue(나중에 store를 store/index.js에 저장)에서 스토어를 Import하려고 하면 main.js의 상태가 변경되지 않고 새로운 스토어가 생성됩니다.
당신의.console.log('app', this.$store)
사용자의 권한 밖입니다.export default {}
그리고 어떤 경우든this.$store
코드는 다음 중 하나에 배치해야 합니다.
computed() {}
mounted()
methods: {}
접속하려고 하는 시점에서this.$store
,this
Vue 인스턴스를 가리키지 않습니다.
Vue 인스턴스의 수명 주기 후크 중 하나에 있는 저장소에 액세스할 수 있습니다(예:created
):
created() {
console.log(this.$store);
}
언급URL : https://stackoverflow.com/questions/48911374/vuex-this-store-is-undefined-in-child-component
반응형
'IT이야기' 카테고리의 다른 글
log4j: 특정 클래스의 출력을 특정 Appender에 기록합니다. (0) | 2022.06.11 |
---|---|
[Nuxt, i18n, Vuex] :접속 방법스토어게터로부터의 $i18n.locales (0) | 2022.06.11 |
포인터에 대한 포인터와 일반 포인터 (0) | 2022.06.10 |
쉼표 연산자의 적절한 사용법은 무엇입니까? (0) | 2022.06.10 |
VueJ 2.0 - 소품 업데이트 시 컴포넌트를 잠글 수 없음 (0) | 2022.06.10 |