현재 개발 환경은 vue2+composition api 이다.
router에서 pinia에 있는 store를 호출하여 값을 꺼내올 일이 생겼는데 자꾸 스토어가 아직 설치되지 않았다는 에러가 발생했다.
컴포넌트 외에서 스토어 사용 시 공홈에서는 아래와 같이 사용하라고 되어있다.
// ❌ 이러면 임포트 안됨
const store = useStore()
router.beforeEach((to, from, next) => {
// we wanted to use the store here
if (store.isLoggedIn) next()
else next('/login')
})
// 이렇게 사용
router.beforeEach((to) => {
// ✅ This will work because the router starts its navigation after
// the router is installed and pinia will be installed too
const store = useStore()
if (to.meta.requiresAuth && !store.isLoggedIn) return '/login'
})
vue-router.esm.js:16 [vue-router] uncaught error during route navigation:
vue-router.esm.js:2314 Error: [🍍]: getActivePinia was called with no active Pinia. Did you forget to install pinia?
찾아보니 나와 비슷한 문제를 겪는 사람들 발견
안녕하세요 올려주신 글 잘보았습니다 그럼 main.js 자체에서도 사용가능할까요?
첫 시작시 토큰이나 특별한 값이 있으면 로그인 처리 하려고 하는데
main에서 처리해야 될것같아서요