IT이야기

Nuxt 저장소를 모듈 모드로 이동하면 'Getter is function' 오류가 발생함

cyworld 2022. 3. 9. 10:02
반응형

Nuxt 저장소를 모듈 모드로 이동하면 'Getter is function' 오류가 발생함

클래식 모드는 곧 더 이상 사용되지 않을 것이기 때문에, 나는 내 스토어를 모듈 모드로 옮기려고 한다.그러나 나는 상태, 행동, 돌연변이 및 게이터를 별도의 파일에 보관하고 싶다.그래서 내가 현재 오직 하나의 모듈만 가지고 있다고 하자 - auth.이게 내 가게 구조야

store
 |_ modules
 |  |_auth
 |    |_actions.js
 |    |_getters.js
 |    |_mutations.js
 |    |_state.js
 |
 |_actions.js
 |_auth.js
 |_getters.js
 |_index.js
 |_mutations.js
 |_state.js

store\modules\auth\state.js현재 하나의 속성만 있음:

export const state = () => {
  return {
    token: null
  }
}

이것은store\modules\auth\getters.js

export const getters = {
  isAuthenticated(state) {
    return !!state.token
  }
}

그럼 내 안에서store\auth.js:

import {actions} from './modules/auth/actions'
import {getters} from './modules/auth/getters'
import {mutations} from './modules/auth/mutations'
import {state} from './modules/auth/state'

export {
  actions,
  getters,
  mutations,
  state
}

그리고 마침내 내 안에store\index.js이 코드만 있어:

export default {
  namespaced: true,
  strict: true
}

이로 인해 다음과 같은 오류가 발생한다.[vuex] getters should be function but "getters.getters" in module "modules.auth" is {}.

나는 지금 몇 시간째 머리를 긁적거리고 있는데 그것을 어떻게 다루어야 할지 모르겠다.

나는 예를 들면 다음과 같은 일을 하려고 했다.

export const getters = () => {
  return {
    isAuthenticated: state => !!state.token
  }
}

그것은 컴파일되었지만 콘솔에서 또 다른 오류를 발생시켰다.[vuex] unknown getter: auth/isAuthenticated

그리고 그것은 또한 나에게 다음과 같은 경고를 주었다.store/modules/auth/state.js should export a method that returns an object

그리고 거기서 나는 내가 그렇게 한다고 생각했다...

좋은 생각이 있으십니까?

마침내 겨우 해결했다.아마 누군가가 도움이 될 것이다.

우선, 나의 게터 수출은 잘못되었다.이렇게 하는 것이 올바른 방법:

export default {
  isAuthenticated(state) {
    return !!state.token
  }
}

주정부도 마찬가지다.

export default () => ({
  token: null
})

그리고 나서 나는 그 집을 옮겨야 했다.auth에서 전송하다.modules아래에 있는 폴더[폴더]에 보관하다store폴더나 또한 제거했다.index.js그리고auth.js파일들

store
 |_auth
 | |_actions.js
 | |_getters.js
 | |_mutations.js
 | |_state.js
 |
 |_actions.js
 |_getters.js
 |_mutations.js
 |_state.js

이제 모든 것이 잘 되었다!

참조URL: https://stackoverflow.com/questions/60889759/moving-nuxt-store-to-modules-mode-generates-getters-should-be-function-error

반응형