반응형
vuex 작업의 오류를 vue 구성 요소로 전파하는 방법
모듈 저장소가 없고 작업 loginUser가 있으며, 권한 부여 헤더를 상태로 저장하여 사용자를 로그인하는 경우:
loginUser(context, user) {
userService.login(user)
.then(response => context.commit('SET_AUTHORIZATION', response.data))
.catch(error => { throw error.response.data })
}
문제는 로그인 컴포넌트의 에러가 필요하고 세션이 활성화되는 한 상점을 끈질기게 만들었기 때문에 해당 상태로 저장할 수 없다는 것이다.로그인 구성 요소의 제출 기능은 다음과 같다.
async onSubmit() {
let user = {
username: this.username,
password: this.password,
}
await this.$store.dispatch('loginUser', user)
.catch(error => {
if(error.response.status === 401) this.loginError = error.message
else console.error(error)
})
await this.$router.push('/').then(() => location.reload())
}
},
이것은 단지 '무공천'을 던져줄 뿐이다.
당신은 당신의 약속으로부터 반환할 수 있다.loginUser
다음과 같은 동작:
loginUser(context, user) {
return new Promise((resolve, reject) => {
userService.login(user)
.then((response) => {
context.commit('SET_AUTHORIZATION', response.data)
resolve(response)
})
.catch((error) => {
reject(error.response.data)
})
})
}
그런 다음 비동기식 업데이트를 할 수 있다.onSubmit()
방법을 사용하여 오류를 포착한다.try..catch
다음과 같다:
async onSubmit() {
try {
let user = {
username: this.username,
password: this.password,
}
await this.$store.dispatch('loginUser', user);
await this.$router.push('/');
location.reload();
} catch (error) {
if (error.response && error.response.status === 401)
this.loginError = error.message
else
console.error(error)
}
},
반응형
'programing' 카테고리의 다른 글
HTML 입력에서 vue.js 데이터로 기본값 로드 (0) | 2022.04.11 |
---|---|
필요한 조언:익스프레스 기능이 있는 VueJS 풀스택 앱 - 백엔드(서버)에서 2개의 스토어, 별도의 Vue 앱 또는 관리자용 익스프레스 app를 사용해야 함 (0) | 2022.04.11 |
Vuex에 있는 다른 이름의 getters에 액세스하는 방법 (0) | 2022.04.11 |
Vue JS - v-for 내부에서 모델링할 선택 옵션 값 바인딩 (0) | 2022.04.11 |
Vuex axi에서 어레이 업데이트 발생 (0) | 2022.04.11 |