반응형
vue.js: 루트 가드가 비동기 값을 기다립니다.
모든 어플리케이션과 마찬가지로 몇 가지 루트가 있습니다.예를 들어 (router/index.js
발췌):
[{
path: '/reporting',
name: 'Reporting',
component: reporting,
meta: {
adminOnly: true
}
},
...]
루트 정의에서 알 수 있듯이 에 액세스 할 때reporting
사용자에게 vuex 저장소의 속성인 관리자 권한이 있어야 합니다.문제:이 속성은 비동기이며 가드에서 처음 액세스할 때는 물론 false입니다.어떻게 하면 방심하지 않고 기다리게 할 수 있을까요?
가드:
if (to.matched.some(route => route.meta.adminOnly)) {
if (store.getters.userInfo.isAdmin) {
next()
} else {
next('/')
}
} else {
next()
}
당신의 getter가 돌아왔다고 가정하면null
데이터를 아직 가져오지 않은 경우.
if (store.getters.userInfo.isAdmin === null) {
const watcher = store.watch(store.getters.userInfo.isAdmin, isAdmin => {
watcher(); // stop watching
if (isAdmin) next();
else next('/');
});
}
else if (store.getters.userInfo.isAdmin) next();
else next('/');
언급URL : https://stackoverflow.com/questions/45479496/vue-js-route-guard-wait-for-async-value
반응형
'programing' 카테고리의 다른 글
vuejs 및 웹 팩에 글꼴 파일을 로드하려면 어떻게 해야 합니까? (0) | 2022.08.25 |
---|---|
Nuxt.js의 axios 플러그인 내에서 현재 fullPath에 액세스하려면 어떻게 해야 합니까? (0) | 2022.08.25 |
Vue 계산 블록이 비동기적으로 동작할 수 있습니까? (0) | 2022.08.25 |
핫 새로고침 기능이 있는 도커 컨테이너의 Vue.js 앱 (0) | 2022.08.25 |
Java에서 int를 이진 문자열 표현으로 변환하시겠습니까? (0) | 2022.08.25 |